Previous 15994 Revisions Next

r6832 Thursday 31st December, 2009 at 02:55:43 UTC by Phill Harvey-Smith
Much improved video code for the nimbus driver,
Scrolling now works, as do some of the welcome demos.
[src/mess/drivers]rmnimbus.c
[src/mess/includes]rmnimbus.h
[src/mess/machine]rmnimbus.c
[src/mess/video]rmnimbus.c

src/mess/video/rmnimbus.c
r6831r6832
55     
66    2009-11-14, P.Harvey-Smith.
77     
8    This is my best guess implementation of the operation of the Nimbus 
9    video system.
10     
11    On the real machine, the Video chip has a block of 64K of memory which is
12    completely seperate from the main 80186 memory. 
13     
14    The main CPU write to the video chip via a series of registers in the 
15    0x0000 to 0x002F reigon, the video chip then manages all video memory 
16    from there.
17     
18    As I cannot find a datasheet for the vide chip marked     
19    MB61H201 Fujitsu RML 12835 GCV, I have had to determine most of its
20    operation by disassembling the Nimbus bios and by writing experemental
21    code on the real machine.   
822*/
923 
1024#include "driver.h"
r6831r6832
1832#include "debug/debugcpu.h"
1933#include "debug/debugcon.h"
2034 
21UINT8 *video_mem;
35UINT8 video_mem[SCREEN_WIDTH_PIXELS][SCREEN_HEIGHT_LINES];
2236 
23#define BYTES_PER_LINE  160
24#define MAX_PIXELX_2BPP 639
25#define MAX_PIXELX_4BPP 319
26 
27#define WIDTH_MASK      0x03
37#define WIDTH_MASK      0x07
2838#define XOR_MASK        0x08
39#define MASK_4080       0x10
2940 
30UINT16  reg002_x;
31UINT16  reg004;
32UINT16  reg010;
33UINT16  reg00C_y;
34UINT16  reg022; 
35UINT16  reg024;
36UINT16  reg026;
37UINT16  reg028;
41// Offsets of nimbus video registers within register array
3842 
43#define NO_VIDREGS      (0x30/2)
44#define reg000          0x00
45#define reg002          0x01
46#define reg004          0x02
47#define reg006          0x03
48#define reg008          0x04
49#define reg00A          0x05
50#define reg00C          0x06
51#define reg00E          0x07
52#define reg010          0x08
53#define reg012          0x09
54#define reg014          0x0A
55#define reg016          0x0B
56#define reg018          0x0C
57#define reg01A          0x0D
58#define reg01C          0x0E
59#define reg01E          0x0F
60#define reg020          0x10
61#define reg022          0x11 
62#define reg024          0x12
63#define reg026          0x13
64#define reg028          0x14
65#define reg02A          0x15
66#define reg02C          0x16
67#define reg02E          0x17
68 
69#define FG_COLOUR       (vidregs[reg024]&0x0F)
70#define BG_COLOUR       ((vidregs[reg024]&0xF0)>>4)
71 
72#define IS_80COL        (vidregs[reg026]&MASK_4080)
73#define IS_XOR          (vidregs[reg022]&XOR_MASK)
74 
75 
76UINT16  vidregs[NO_VIDREGS];
77 
3978UINT8   bpp;            // Bits / pixel
4079UINT8   ppb;            // Pixels / byte
80UINT16  pixel_mask;
4181UINT8   border_colour;
4282 
4383int debug_on;
4484 
45static void set_pixel(UINT16 x, UINT16 y, UINT8 on_off);
46static void write_pixel_line(UINT16 x, UINT16 y, UINT16    data, UINT16    mem_mask, UINT8 width);
47static void write_pixel_data(UINT16 x, UINT16 y, UINT16    data, UINT16    mem_mask);
48static void write_reg_004(UINT16    data, UINT16    mem_mask);
49static void write_reg_010(UINT16    data, UINT16    mem_mask);
50static void write_reg_014(UINT16    data, UINT16    mem_mask);
51static void write_reg_01A(UINT16    data, UINT16    mem_mask);
52static void write_reg_026(UINT16    data, UINT16    mem_mask);
85static void set_pixel(UINT16 x, UINT16 y, UINT8 colour);
86static void write_pixel_line(UINT16 x, UINT16 y, UINT16    data, UINT8 width);
87static void write_pixel_data(UINT16 x, UINT16 y, UINT16    data);
88static void move_pixel_line(UINT16 x, UINT16 y, UINT16    data, UINT8 width);
89static void write_reg_004(void);
90static void write_reg_010(void);
91static void write_reg_012(void);
92static void write_reg_014(void);
93static void write_reg_01A(void);
94static void write_reg_026(void);
5395 
5496static void video_debug(running_machine *machine, int ref, int params, const char *param[]);
97static void video_regdump(running_machine *machine, int ref, int params, const char *param[]);
5598 
99/*
100    I'm not sure which of thes return values on a real machine, so for the time being I'm going
101    to return the values for all of them, it doesn't seem to hurt !
102*/
103 
56104READ16_HANDLER (nimbus_video_io_r)
57105{
58106    int     pc=cpu_get_pc(space->cpu);
59107    UINT16  result;
60108         
61    switch (offset*2)
109    switch (offset)
62110    {
63        case    0x002   : result=reg002_x; break;
64        case    0x004   : result=reg004; break;
65        case    0x00C   : result=reg00C_y; break;
66        case    0x010   : result=reg010; break;
67        case    0x022   : result=reg022; break;
68        case    0x024   : result=reg024; break;
69        case    0x026   : result=reg024; break;
70        case    0x028   : result=reg028; break;
111        case    reg000  : result=vidregs[reg000]; break;
112        case    reg002  : result=vidregs[reg002]; break;
113        case    reg004  : result=vidregs[reg004]; break;
114        case    reg006  : result=vidregs[reg006]; break;
115        case    reg008  : result=vidregs[reg008]; break;
116        case    reg00A  : result=vidregs[reg00A]; break;
117        case    reg00C  : result=vidregs[reg00C]; break;
118        case    reg00E  : result=vidregs[reg00E]; break;
119 
120        case    reg010  : result=vidregs[reg010]; break;
121        case    reg012  : result=vidregs[reg012]; break;
122        case    reg014  : result=vidregs[reg014]; break;
123        case    reg016  : result=vidregs[reg016]; break;
124        case    reg018  : result=vidregs[reg018]; break;
125        case    reg01A  : result=vidregs[reg01A]; break;
126        case    reg01C  : result=vidregs[reg01C]; break;
127        case    reg01E  : result=vidregs[reg01E]; break;
128 
129        case    reg020  : result=vidregs[reg020]; break;
130        case    reg022  : result=vidregs[reg022]; break;
131        case    reg024  : result=vidregs[reg024]; break;
132        case    reg026  : result=vidregs[reg026]; break;
133        case    reg028  : result=vidregs[reg028]; break;
134        case    reg02A  : result=vidregs[reg02A]; break;
135        case    reg02C  : result=vidregs[reg02C]; break;
136        case    reg02E  : result=vidregs[reg02E]; break;
71137        default         : result=0; break;
72138    }
73139 
r6831r6832
77143    return result;
78144}
79145 
146/*
147    Write to the video registers, the default action is to write to the array of registers.
148    If a register also needs some special action call the action function for that register.
149     
150    Incase anyone wonders about the DEBUG_DB statement, this allows me to log which registers 
151    are being written to and then play them back at the real machine, this has helped greatly
152    in figuring out what the video registers do.
153     
154*/
155 
80156WRITE16_HANDLER (nimbus_video_io_w)
81157{
82158    int pc=cpu_get_pc(space->cpu);
r6831r6832
88164        logerror("dw %05X,%05X\n",(offset*2),data);
89165     
90166     
91    switch (offset*2)
167    switch (offset)
92168    {
93        case    0x002   : COMBINE_DATA(&reg002_x); break;
94        case    0x004   : write_reg_004(data,mem_mask); break;
95        case    0x00C   : COMBINE_DATA(&reg00C_y); break;
96        case    0x010   : write_reg_010(data,mem_mask); break;
97        case    0x014   : write_reg_014(data,mem_mask); break;
98        case    0x01A   : write_reg_01A(data,mem_mask); break;
99        case    0x022   : COMBINE_DATA(&reg022); break;
100        case    0x024   : COMBINE_DATA(&reg024); break;
101        case    0x026   : write_reg_026(data,mem_mask); break;
102        case    0x028   : COMBINE_DATA(&reg028); break;
169        case    reg000  : vidregs[reg000]=data; break;
170        case    reg002  : vidregs[reg002]=data; break;
171        case    reg004  : vidregs[reg004]=data; write_reg_004(); break;
172        case    reg006  : vidregs[reg006]=data; break;
173        case    reg008  : vidregs[reg008]=data; break;
174        case    reg00A  : vidregs[reg00A]=data; break;
175        case    reg00C  : vidregs[reg00C]=data; break;
176        case    reg00E  : vidregs[reg00E]=data; break;
177 
178        case    reg010  : vidregs[reg010]=data; write_reg_010(); break;
179        case    reg012  : vidregs[reg012]=data; write_reg_012(); break;
180        case    reg014  : vidregs[reg014]=data; write_reg_014(); break;
181        case    reg016  : vidregs[reg016]=data; break;
182        case    reg018  : vidregs[reg018]=data; break;
183        case    reg01A  : vidregs[reg01A]=data; write_reg_01A(); break;
184        case    reg01C  : vidregs[reg01C]=data; break;
185        case    reg01E  : vidregs[reg01E]=data; break;
186 
187        case    reg020  : vidregs[reg020]=data; break;
188        case    reg022  : vidregs[reg022]=data; break;
189        case    reg024  : vidregs[reg024]=data; break;
190        case    reg026  : vidregs[reg026]=data; write_reg_026(); break;
191        case    reg028  : vidregs[reg028]=data; break;
192        case    reg02A  : vidregs[reg02A]=data; break;
193        case    reg02C  : vidregs[reg02C]=data; break;
194        case    reg02E  : vidregs[reg02E]=data; break;
195         
103196        default         : break;
104197    }
105198}
106199 
107static void set_pixel(UINT16 x, UINT16 y, UINT8 on_off )
200static void set_pixel(UINT16 x, UINT16 y, UINT8 colour)
108201{
109    offs_t  vaddr;
110    UINT8   rshifts;
111    UINT8   lshifts;
112    UINT8   last;
113    UINT8   mask;
114    UINT8   pixel_offset;
115    UINT8   colour;
202    if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
203        logerror("set_pixel(x=%04X, y=%04X, colour=%04X), IS_XOR=%02X\n",x,y,colour,IS_XOR);
116204     
117    vaddr=(BYTES_PER_LINE*y)+(x / ppb);
118    pixel_offset=(x % ppb);
119    last=(vaddr<VRAM_SIZE) ? video_mem[vaddr] : 0
120     
121    ;
122    colour=on_off ? (reg024&0x0F) : ((reg024&0xF0)>>4);
123     
124    if(bpp==2)
205 
206    if((x<SCREEN_WIDTH_PIXELS) && (y<SCREEN_HEIGHT_LINES))
125207    {
126        colour&=0x03;
127        rshifts=(pixel_offset%4)*2;
128        mask=~(0xC0>>rshifts);
129        lshifts=6-rshifts;
130    }
131    else
132    {
133        colour&=0x0F;
134        rshifts=(pixel_offset%2)*4;
135        mask=~(0xF0>>rshifts);
136        lshifts=4-rshifts;         
137    }
138     
139    if(vaddr<VRAM_SIZE)
140    {
141        if(reg022 & XOR_MASK)
142            video_mem[vaddr]=(video_mem[vaddr] & mask) ^ (colour<<(lshifts)); 
208        if(IS_XOR)
209            video_mem[x][y]^=colour;
143210        else
144            video_mem[vaddr]=(video_mem[vaddr] & mask) | (colour<<(lshifts)); 
145 
146        if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
147            logerror("set_pixel(X=%04X, y=%04x, pixel_offset=%02X, on_off=%02X), vaddr=%04X, mask=%02X, last=%02X, current=%02X\n",x,y,pixel_offset,on_off,vaddr,mask,last,video_mem[vaddr]);
211            video_mem[x][y]=colour;
148212    }
149    else
150    {
151        if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
152            logerror("ERROR: vaddr=%05X > VRAM_SIZE=%05X\n",vaddr,VRAM_SIZE);
153    }
154213}
155214 
156 
157static void write_pixel_line(UINT16 x, UINT16 y, UINT16    data, UINT16    mem_mask, UINT8 width)
215static void write_pixel_line(UINT16 x, UINT16 y, UINT16    data, UINT8 width)
158216{
159    UINT8  bit;
217    UINT16  mask;
160218    UINT16  pixel_x;
219    UINT16  colour;
161220    UINT8   shifts;
162221 
163222    if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
164        logerror("write_pixel_line(x=%04X, y=%04X, data=%04X, mem_mask=%04X,width=%02X)\n",x,y,data,mem_mask,width);
223        logerror("write_pixel_line(x=%04X, y=%04X, data=%04X, width=%02X, bpp=%02X, pixel_mask=%02X)\n",x,y,data,width,bpp,pixel_mask);
165224     
166    shifts=(width==4) ? 2 : 1;     
225    shifts=width-bpp;     
167226     
168    for(bit=0x80, pixel_x=(x*width); bit>0; bit=(bit>>shifts), pixel_x++)
227    for(mask=pixel_mask, pixel_x=(x*(width/bpp)); mask>0; mask=(mask>>bpp), pixel_x++)
169228    {
170        set_pixel(pixel_x,y,(data&bit));
171        if(width==16)
172            set_pixel(pixel_x+1,y,(data&bit));
229        if(bpp==1)
230            colour=(data & mask) ? FG_COLOUR : BG_COLOUR;
231        else
232            colour=(data & mask) >> shifts;
233         
234        //logerror("write_pixel_line: data=%04X, mask=%04X, shifts=%02X, bpp=%02X colour=%02X\n",data,mask,shifts,bpp,colour); 
235         
236        if(IS_80COL)
237        {
238            set_pixel(pixel_x,y,colour);
239        }
240        else
241        {
242            set_pixel((pixel_x*2),y,colour);
243            set_pixel((pixel_x*2)+1,y,colour);
244        }
245        shifts-=bpp;
173246    }
174247}
175248 
176static void write_pixel_data(UINT16 x, UINT16 y, UINT16    data, UINT16    mem_mask)
249static void move_pixel_line(UINT16 x, UINT16 y, UINT16    data, UINT8 width)
250{
251    UINT16  pixelno;
252    UINT16  pixelx;
253     
254    if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
255       logerror("move_pixel_line(x=%04X, y=%04X, data=%04X, width=%02X)\n",x,y,data,width);
256       
257    for(pixelno=0;pixelno<width;pixelno++)
258    {
259        pixelx=(x*width)+pixelno;
260        if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
261            logerror("pixelx=%04X\n",pixelx);
262        video_mem[pixelx][vidregs[reg020]]=video_mem[pixelx][y];
263    }
264}
265 
266 
267/* 
268    The values in the bottom 3 bits of reg022 seem to determine the number of bits per pixel
269    for following operations.
270     
271    The values that I have decoded so far are :
272     
273    000 1bpp, foreground and background colours taken from reg024
274    001 2bpp, using the first 4 colours of the pallette
275    010 
276    011 
277    100 4bpp, must be a 16 bit word, of which the upper byte is a mask anded with the lower byte
278              containing the pixel data for two pixels.
279    101
280    110 4bpp, 16 bit word containing the pixel data for 4 pixels.
281    111
282     
283    Bit 3 of reg022 is as follows :
284     
285    0   pixels are written from supplied colour data
286    1   pixels are xor'ed onto the screen
287*/
288 
289static void write_pixel_data(UINT16 x, UINT16 y, UINT16    data)
177290{     
178291    if(debug_on & (DEBUG_TEXT | DEBUG_PIXEL))
179        logerror("write_pixel_data(x=%04X, y=%04X, data=%04X, mem_mask=%04X), reg022=%04X\n",x,y,data,mem_mask,reg022);
292        logerror("write_pixel_data(x=%04X, y=%04X, data=%04X), reg022=%04X\n",x,y,data,vidregs[reg022]);
180293     
181    switch (reg022 & WIDTH_MASK)
294    if(IS_80COL)
182295    {
183        case 0x00   : write_pixel_line(x,y,data,mem_mask,16); break;
184        case 0x01   : write_pixel_line(x,y,data,mem_mask,8); break;
185        case 0x02   : write_pixel_line(x,y,data,mem_mask,8); break;
186        case 0x03   : set_pixel(x,y,(reg024&0x0F)); break;       
296        switch (vidregs[reg022] & WIDTH_MASK)
297        {
298            case 0x00   : bpp=1; pixel_mask=0x8000;
299                          write_pixel_line(x,y,data,16); 
300                          break;
301                       
302            case 0x01   : bpp=1; pixel_mask=0x80;
303                          write_pixel_line(x,y,data,8); 
304                          break;
305                       
306            case 0x02   : bpp=1; pixel_mask=0x0080;
307                          write_pixel_line(x,y,data,8); break;
308         
309            case 0x03   : bpp=1; 
310                          set_pixel(x,y,FG_COLOUR); 
311                          break; 
312         
313            case 0x04   : bpp=2; pixel_mask=0xC0;
314                          write_pixel_line(x,y,((data & 0xFF) & ((data & 0xFF00)>>8)),8); 
315                          break;
316         
317            case 0x05   : move_pixel_line(x,y,data,8);
318                          break;
319         
320            case 0x06   : bpp=2; pixel_mask=0xC000;
321                          write_pixel_line(x,y,data,16); 
322                          break;
323         
324            case 0x07   : bpp=1; 
325                          set_pixel(x,y,FG_COLOUR);
326                          break;   
327        }
187328    }
329    else
330    {
331        switch (vidregs[reg022] & WIDTH_MASK)
332        {
333            case 0x00   : bpp=1; pixel_mask=0x0080;
334                          write_pixel_line(x,y,data,8); 
335                          break;
336                       
337            case 0x01   : bpp=2; pixel_mask=0xC0;
338                          write_pixel_line(x,y,data,8); 
339                          break;
340                       
341            case 0x02   : bpp=1; pixel_mask=0x0080;
342                          write_pixel_line(x,y,data,8); break;
343         
344            case 0x03   : bpp=1; 
345                          set_pixel(x,y,FG_COLOUR); 
346                          break; 
347         
348            case 0x04   : bpp=4; pixel_mask=0xF0;
349                          write_pixel_line(x,y,((data & 0xFF) & ((data & 0xFF00)>>8)),8); 
350                          break;
351         
352            case 0x05   : move_pixel_line(x,y,data,16);
353                          break;
354         
355            case 0x06   : bpp=4; pixel_mask=0xF000;
356                          write_pixel_line(x,y,data,16); 
357                          break;
358         
359            case 0x07   : bpp=1; 
360                          set_pixel(x,y,FG_COLOUR);
361                          break;   
362        }
363    }
188364}
189365 
190static void write_reg_004(UINT16    data, UINT16    mem_mask)
366static void write_reg_004(void)
191367{
192    COMBINE_DATA(&reg004);
193     
194    reg002_x=1;
195    reg00C_y++;
368    vidregs[reg002]=0;
369    vidregs[reg00C]++;
196370}
197371 
198static void write_reg_010(UINT16    data, UINT16    mem_mask)
372static void write_reg_010(void)
199373{
200    write_pixel_data(0,reg00C_y,data,mem_mask);     
374    write_pixel_data(0,vidregs[reg00C],vidregs[reg010]);     
201375}
202376 
203static void write_reg_014(UINT16    data, UINT16    mem_mask)
377static void write_reg_012(void)
378{
379    // I dunno if this is actually what is happening as the regs seem to be write only....
380    // doing this however does seem to make some programs (worms from the welcom disk)
381    // work correctly.
382    if(IS_80COL)
383        vidregs[reg002]=vidregs[reg012];
384    else
385        vidregs[reg002]=vidregs[reg012]/2; 
386 
387    write_pixel_data(vidregs[reg012],vidregs[reg00C],FG_COLOUR);
388}
389 
390static void write_reg_014(void)
204391{   
205    write_pixel_data(reg002_x+1,reg00C_y,data,mem_mask);
206     
207    reg00C_y++;
392    write_pixel_data(vidregs[reg002],++vidregs[reg00C],vidregs[reg014]);
208393}
209394 
210static void write_reg_01A(UINT16    data, UINT16    mem_mask)
395static void write_reg_01A(void)
211396{
212    write_pixel_data(reg002_x+1,reg00C_y,data,mem_mask);   
213     
214    reg002_x++;
397    write_pixel_data(++vidregs[reg002],vidregs[reg00C],vidregs[reg01A]);   
215398}
216399 
217static void write_reg_026(UINT16    data, UINT16    mem_mask)
400/*
401    bits 0..3 of reg026 contain the border colour.
402    bit 5 contains the 40/80 column (320/640 pixel) flag.
403*/
404 
405static void write_reg_026(void)
218406{
219    COMBINE_DATA(&reg026);
407    border_colour=vidregs[reg026] & 0x0F;
220408 
221    switch (data & 0x30)
222    {
223        case    0x00    : bpp=4;
224        case    0x10    : bpp=2;
225        case    0x20    : bpp=2;
226        case    0x30    : bpp=2;         
227    }
228 
229    ppb=(8/bpp);
230 
231    border_colour=data & 0x0F;
232 
233409    if(debug_on & DEBUG_TEXT)
234        logerror("reg 026 write, bpp=%02X, border_colour=%02X\n",bpp,border_colour);
410        logerror("reg 026 write, border_colour=%02X\n",border_colour);
235411}
236412 
237413static void video_debug(running_machine *machine, int ref, int params, const char *param[])
r6831r6832
244420    {
245421        debug_console_printf(machine,"Error usage : nimbus_vid_debug <debuglevel>\n");
246422        debug_console_printf(machine,"Current debuglevel=%02X\n",debug_on);
423        debug_console_printf(machine,"VRAM size=%02X\n",VRAM_SIZE);
247424    }
248425}
249426 
427static void video_regdump(running_machine *machine, int ref, int params, const char *param[])
428{
429    int regno;
430     
431    for(regno=0;regno<0x08;regno++)
432        debug_console_printf(machine,"reg%03X=%04X reg%03X=%04X reg%03X=%04X\n",
433                regno*2,vidregs[regno],
434                (regno+0x08)*2,vidregs[regno+0x08],
435                (regno+0x10)*2,vidregs[regno+0x10]);
436}
250437 
251438VIDEO_START( nimbus )
252439{
253440    debug_on=0;
254441     
255    reg002_x=0x0000;
256    reg00C_y=0x0000;
257     
258    reg022=0x0000;
259    reg024=0x0000;
260    reg028=0x0000;
261    bpp=4;          // bits per pixel
262 
263442    logerror("VIDEO_START\n");
264    video_mem=(UINT8 *)memory_region_alloc(machine,VRAM_NAME,VRAM_SIZE,ROMREGION_8BIT);
265 
266    memset(video_mem,0,VRAM_SIZE); 
267     
443   
268444      if (machine->debug_flags & DEBUG_FLAG_ENABLED)
269445   {
270446        debug_console_register_command(machine, "nimbus_vid_debug", CMDFLAG_NONE, 0, 0, 1, video_debug);
447        debug_console_register_command(machine, "nimbus_vid_regdump", CMDFLAG_NONE, 0, 0, 1, video_regdump);
271448    }
272449}
273450 
451VIDEO_RESET( nimbus )
452{
453    // When we reset clear the video registers and video memory.
454    memset(&vidregs,0x00,sizeof(vidregs));
455    memset(&video_mem,0,sizeof(video_mem)); 
456 
457    bpp=4;          // bits per pixel
458    logerror("Video reset\n");
459}
460 
274461VIDEO_EOF( nimbus )
275462{
276463 
r6831r6832
281468{
282469    int     XCoord;
283470    int     YCoord = video_screen_get_vpos(screen);
284    offs_t  vaddr;
285    UINT8   pixel_offset;
286    UINT8   shifts;
287    UINT8   pixel;
288     
289    UINT16  xmax;
290 
291    if (bpp==2) 
292        xmax=MAX_PIXELX_2BPP;
293    else
294        xmax=MAX_PIXELX_4BPP;
295 
296    vaddr=(YCoord*BYTES_PER_LINE)-1;
297     
298    for(XCoord=0;XCoord<xmax;XCoord++)
299    {
300        if ((XCoord % (8/bpp))==0)
301            vaddr++;
302             
303        pixel_offset=(XCoord % 8);
304             
305        if(bpp==2)
306        {
307            shifts=(pixel_offset%4)*2;
308            pixel=(video_mem[vaddr] & (0xC0>>shifts)) >> (6-shifts);
309        }
310        else
311        {
312            shifts=(pixel_offset%2)*4;
313            pixel=(video_mem[vaddr] & (0xF0>>shifts)) >> (4-shifts);
314        }
315             
316        *BITMAP_ADDR16(bitmap, YCoord, XCoord)=pixel;
471     
472    for(XCoord=0;XCoord<SCREEN_WIDTH_PIXELS;XCoord++)
473    {       
474        *BITMAP_ADDR16(bitmap, YCoord, XCoord)=video_mem[XCoord][YCoord];
317475    }
318476 
319    reg028++;
320    if((reg028 & 0x000F)>0x0A)
321        reg028&=0xFFF0;
477    vidregs[reg028]++;
478    if((vidregs[reg028] & 0x000F)>0x0A)
479        vidregs[reg028]&=0xFFF0;
322480 
323 
324481    return 0;
325482}
326483 
src/mess/includes/rmnimbus.h
r6831r6832
8585 
8686#define NO_DRIVE_SELECTED   0xFF
8787 
88 
8988/* Video stuff */
9089READ16_HANDLER (nimbus_video_io_r);
9190WRITE16_HANDLER (nimbus_video_io_w);
r6831r6832
9392VIDEO_START( nimbus );
9493VIDEO_EOF( nimbus );
9594VIDEO_UPDATE( nimbus );
95VIDEO_RESET( nimbus );
9696 
97#define VRAM_NAME   "nimbusvram"
98#define VRAM_SIZE   (64*1024)
97#define SCREEN_WIDTH_PIXELS     640
98#define SCREEN_HEIGHT_LINES     250
9999 
100#define BYTES_PER_LINE          (SCREEN_WIDTH_PIXELS/2)
101#define PIXELS_PER_BYTE         2
100102 
101103#define LINEAR_ADDR(seg,ofs)    ((seg<<4)+ofs)
102104 
r6831r6832
135137} t_nimbus_brush;
136138 
137139#define OUTPUT_SEGOFS(mess,seg,ofs)  logerror("%s=%04X:%04X [%08X]\n",mess,seg,ofs,((seg<<4)+ofs))
138 
139 
140/*
141 
142    Grapics registers (best guesses)
143     
144    Addr    width   purpose
145     
146    0x000C  word    Y co-ordinate 
147    0x0012  word    X co-ordinate
148     
149    0x0014  byte
150    0x001A  word
151     
152*/
src/mess/machine/rmnimbus.c
r6831r6832
1/*
2    machine/rmnimbus.c
3
4    Machine driver for the Research Machines Nimbus.
5
6    Phill Harvey-Smith
7    2009-11-29.
8
9    80186 internal DMA/Timer/PIC code borrowed from Compis driver.
10    Perhaps this needs merging into the 80186 core.....
11*/
12
13
14#include "driver.h"
15#include "memory.h"
16#include "cpu/i86/i86.h"
17#include "debug/debugcpu.h"
18#include "debug/debugcon.h"
19#include "devices/flopdrv.h"
20#include "machine/pic8259.h"
21#include "machine/pit8253.h"
22#include "machine/msm8251.h"
23#include "machine/z80sio.h"
24
25#include "includes/rmnimbus.h"
26
27/*-------------------------------------------------------------------------*/
28/* Defines, constants, and global variables                                */
29/*-------------------------------------------------------------------------*/
30
31/* CPU 80186 */
32#define LATCH_INTS          1
33#define LOG_PORTS           0
34#define LOG_INTERRUPTS      0
35#define LOG_INTERRUPTS_EXT  0
36#define LOG_TIMER           0
37#define LOG_OPTIMIZATION    0
38#define LOG_DMA             0
39#define CPU_RESUME_TRIGGER   7123
40#define LOG_KEYBOARD        1
41
42/* 80186 internal stuff */
43struct mem_state
44{
45   UINT16       lower;
46   UINT16       upper;
47   UINT16       middle;
48   UINT16       middle_size;
49   UINT16       peripheral;
50};
51
52struct timer_state
53{
54   UINT16       control;
55   UINT16       maxA;
56   UINT16       maxB;
57   UINT16       count;
58   emu_timer   *int_timer;
59   emu_timer   *time_timer;
60   UINT8       time_timer_active;
61   attotime   last_time;
62};
63
64struct dma_state
65{
66   UINT32       source;
67   UINT32       dest;
68   UINT16       count;
69   UINT16       control;
70   UINT8       finished;
71   emu_timer   *finish_timer;
72};
73
74struct intr_state
75{
76   UINT8   pending;
77   UINT16   ack_mask;
78   UINT16   priority_mask;
79   UINT16   in_service;
80   UINT16   request;
81   UINT16   status;
82   UINT16   poll_status;
83   UINT16   timer;
84   UINT16   dma[2];
85   UINT16   ext[4];
86    UINT16  ext_vector[2]; // external vectors, when in cascade mode
87};
88
89static struct i186_state
90{
91   struct timer_state   timer[3];
92   struct dma_state   dma[2];
93   struct intr_state   intr;
94   struct mem_state   mem;
95} i186;
96
97
98/* Z80 SIO */
99
100const z80sio_interface sio_intf =
101{
102   sio_interrupt,         /* interrupt handler */
103   0, //sio_dtr_w,            /* DTR changed handler */
104   0,                  /* RTS changed handler */
105   0,                  /* BREAK changed handler */
106   sio_serial_transmit,   /* transmit handler */
107   sio_serial_receive      /* receive handler */
108};
109
110struct keybord_state
111{
112    UINT8       keyrows[NIMBUS_KEYROWS];
113    emu_timer   *keyscan_timer;
114    UINT8       queue[KEYBOARD_QUEUE_SIZE];
115   UINT8       head;
116   UINT8       tail;
117} keyboard;
118
119
120
121/* Floppy drives WD2793 */
122
123static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_intrq_w );
124static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_drq_w );
125
126const wd17xx_interface nimbus_wd17xx_interface =
127{
128   DEVCB_LINE(nimbus_fdc_intrq_w),
129   DEVCB_LINE(nimbus_fdc_drq_w),
130   {FLOPPY_0, FLOPPY_1, FLOPPY_2, FLOPPY_3}
131};
132
133static struct _nimbus_fdc
134{
135    UINT8 drq_enabled;
136    UINT8 irq_enabled;
137    UINT8 diskno;
138    UINT8 motor_on;
139} nimbus_fdc;
140
141
142static void drq_callback(running_machine *machine, int which);
143
144static void execute_debug_irq(running_machine *machine, int ref, int params, const char *param[]);
145static void execute_debug_intmasks(running_machine *machine, int ref, int params, const char *param[]);
146
147static int instruction_hook(const device_config *device, offs_t curpc);
148static void decode_subbios(const device_config *device,offs_t pc);
149static void decode_dssi_f_fill_area(const device_config *device,UINT16  ds, UINT16 si);
150static void decode_dssi_f_plot_character_string(const device_config *device,UINT16  ds, UINT16 si);
151static void decode_dssi_f_set_new_clt(const device_config *device,UINT16  ds, UINT16 si);
152static void fdc_reset(void);
153
154static void keyboard_reset(void);
155static TIMER_CALLBACK(keyscan_callback);
156
157#define num_ioports 0x80
158static UINT16   IOPorts[num_ioports];
159
160UINT8 nextkey;
161UINT8 sio_int_state;
162
163/*************************************
164 *
165 *  80186 interrupt controller
166 *
167 *************************************/
168static IRQ_CALLBACK(int_callback)
169{
170    UINT8   vector;
171    UINT16  old;
172    UINT16  oldreq;
173
174   if (LOG_INTERRUPTS)
175      logerror("(%f) **** Acknowledged interrupt vector %02X\n", attotime_to_double(timer_get_time(device->machine)), i186.intr.poll_status & 0x1f);
176
177   /* clear the interrupt */
178   cpu_set_input_line(device, 0, CLEAR_LINE);
179   i186.intr.pending = 0;
180
181    oldreq=i186.intr.request;
182
183   /* clear the request and set the in-service bit */
184#if LATCH_INTS
185   i186.intr.request &= ~i186.intr.ack_mask;
186#else
187   i186.intr.request &= ~(i186.intr.ack_mask & 0x0f);
188#endif
189
190    if(i186.intr.request!=oldreq)
191        logerror("i186.intr.request changed from %02X to %02X\n",oldreq,i186.intr.request);
192
193    old=i186.intr.in_service;
194
195   i186.intr.in_service |= i186.intr.ack_mask;
196
197    if (LOG_INTERRUPTS)
198        if(i186.intr.in_service!=old)
199            logerror("i186.intr.in_service changed from %02X to %02X\n",old,i186.intr.in_service);
200
201    if (i186.intr.ack_mask == 0x0001)
202   {
203      switch (i186.intr.poll_status & 0x1f)
204      {
205         case 0x08:   i186.intr.status &= ~0x01;   break;
206         case 0x12:   i186.intr.status &= ~0x02;   break;
207         case 0x13:   i186.intr.status &= ~0x04;   break;
208      }
209   }
210   i186.intr.ack_mask = 0;
211
212   /* a request no longer pending */
213   i186.intr.poll_status &= ~0x8000;
214
215   /* return the vector */
216    switch(i186.intr.poll_status & 0x1F)
217    {
218        case 0x0C   : vector=(i186.intr.ext[0] & EXTINT_CTRL_CASCADE) ? i186.intr.ext_vector[0] : (i186.intr.poll_status & 0x1f); logerror("int 0x0c vector=%02X\n",vector); break;
219        case 0x0D   : vector=(i186.intr.ext[1] & EXTINT_CTRL_CASCADE) ? i186.intr.ext_vector[1] : (i186.intr.poll_status & 0x1f); logerror("int 0x0D vector=%02X\n",vector); break;
220        default :
221            vector=i186.intr.poll_status & 0x1f; break;
222    }
223
224    if (LOG_INTERRUPTS_EXT)
225   {
226        logerror("i186.intr.ext[0]=%04X i186.intr.ext[1]=%04X\n",i186.intr.ext[0],i186.intr.ext[1]);
227        logerror("Ext vectors : %02X %02X\n",i186.intr.ext_vector[0],i186.intr.ext_vector[1]);
228        logerror("Calling vector %02X\n",vector);
229    }
230
231    return vector;
232}
233
234
235static void update_interrupt_state(running_machine *machine)
236{
237    int new_vector = 0;
238    int Priority;
239    int IntNo;
240
241   if (LOG_INTERRUPTS)
242        logerror("update_interrupt_status: req=%04X stat=%04X serv=%04X priority_mask=%4X\n", i186.intr.request, i186.intr.status, i186.intr.in_service, i186.intr.priority_mask);
243
244   /* loop over priorities */
245   for (Priority = 0; Priority <= i186.intr.priority_mask; Priority++)
246   {
247      /* note: by checking 4 bits, we also verify that the mask is off */
248      if ((i186.intr.timer & 15) == Priority)
249      {
250         /* if we're already servicing something at this level, don't generate anything new */
251         if (i186.intr.in_service & 0x01)
252            return;
253
254         /* if there's something pending, generate an interrupt */
255         if (i186.intr.status & 0x07)
256         {
257            if (i186.intr.status & 1)
258               new_vector = 0x08;
259            else if (i186.intr.status & 2)
260               new_vector = 0x12;
261            else if (i186.intr.status & 4)
262               new_vector = 0x13;
263            else
264               popmessage("Invalid timer interrupt!");
265
266            /* set the clear mask and generate the int */
267            i186.intr.ack_mask = 0x0001;
268            goto generate_int;
269         }
270      }
271
272      /* check DMA interrupts */
273      for (IntNo = 0; IntNo < 2; IntNo++)
274         if ((i186.intr.dma[IntNo] & 0x0F) == Priority)
275         {
276            /* if we're already servicing something at this level, don't generate anything new */
277            if (i186.intr.in_service & (0x04 << IntNo))
278               return;
279
280            /* if there's something pending, generate an interrupt */
281            if (i186.intr.request & (0x04 << IntNo))
282            {
283               new_vector = 0x0a + IntNo;
284
285               /* set the clear mask and generate the int */
286               i186.intr.ack_mask = 0x0004 << IntNo;
287               goto generate_int;
288            }
289         }
290
291        //logerror("Checking external ints\n");
292      /* check external interrupts */
293      for (IntNo = 0; IntNo < 4; IntNo++)
294         if ((i186.intr.ext[IntNo] & 0x0F) == Priority)
295         {
296                if (LOG_INTERRUPTS)
297                    logerror("Int%d priority=%d\n",IntNo,Priority);
298
299                /* if we're already servicing something at this level, don't generate anything new */
300            if (i186.intr.in_service & (0x10 << IntNo))
301               return;
302
303            /* if there's something pending, generate an interrupt */
304            if (i186.intr.request & (0x10 << IntNo))
305            {
306               /* otherwise, generate an interrupt for this request */
307               new_vector = 0x0c + IntNo;
308
309               /* set the clear mask and generate the int */
310               i186.intr.ack_mask = 0x0010 << IntNo;
311                    logerror("External int !\n");
312               goto generate_int;
313            }
314         }
315   }
316   return;
317
318generate_int:
319   /* generate the appropriate interrupt */
320   i186.intr.poll_status = 0x8000 | new_vector;
321   if (!i186.intr.pending)
322      cputag_set_input_line(machine, MAINCPU_TAG, 0, ASSERT_LINE);
323   i186.intr.pending = 1;
324   cpuexec_trigger(machine, CPU_RESUME_TRIGGER);
325   if (LOG_OPTIMIZATION) logerror("  - trigger due to interrupt pending\n");
326   if (LOG_INTERRUPTS) logerror("(%f) **** Requesting interrupt vector %02X\n", attotime_to_double(timer_get_time(machine)), new_vector);
327}
328
329
330static void handle_eoi(running_machine *machine,int data)
331{
332    int Priority;
333    int IntNo;
334
335   /* specific case */
336   if (!(data & 0x8000))
337   {
338      /* turn off the appropriate in-service bit */
339      switch (data & 0x1f)
340      {
341         case 0x08:   i186.intr.in_service &= ~0x01;   break;
342         case 0x12:   i186.intr.in_service &= ~0x01;   break;
343         case 0x13:   i186.intr.in_service &= ~0x01;   break;
344         case 0x0a:   i186.intr.in_service &= ~0x04;   break;
345         case 0x0b:   i186.intr.in_service &= ~0x08;   break;
346         case 0x0c:   i186.intr.in_service &= ~0x10;   break;
347         case 0x0d:   i186.intr.in_service &= ~0x20;   break;
348         case 0x0e:   i186.intr.in_service &= ~0x40;   break;
349         case 0x0f:   i186.intr.in_service &= ~0x80;   break;
350         default:   logerror("%05X:ERROR - 80186 EOI with unknown vector %02X\n", cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), data & 0x1f);
351      }
352      if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for vector %02X\n", attotime_to_double(timer_get_time(machine)), data & 0x1f);
353   }
354
355   /* non-specific case */
356   else
357   {
358      /* loop over priorities */
359      for (Priority = 0; Priority <= 7; Priority++)
360      {
361         /* check for in-service timers */
362         if ((i186.intr.timer & 0x07) == Priority && (i186.intr.in_service & 0x01))
363         {
364            i186.intr.in_service &= ~0x01;
365            if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for timer\n", attotime_to_double(timer_get_time(machine)));
366            return;
367         }
368
369         /* check for in-service DMA interrupts */
370         for (IntNo = 0; IntNo < 2; IntNo++)
371            if ((i186.intr.dma[IntNo] & 0x07) == Priority && (i186.intr.in_service & (0x04 << IntNo)))
372            {
373               i186.intr.in_service &= ~(0x04 << IntNo);
374               if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for DMA%d\n", attotime_to_double(timer_get_time(machine)), IntNo);
375               return;
376            }
377
378         /* check external interrupts */
379         for (IntNo = 0; IntNo < 4; IntNo++)
380            if ((i186.intr.ext[IntNo] & 0x07) == Priority && (i186.intr.in_service & (0x10 << IntNo)))
381            {
382               i186.intr.in_service &= ~(0x10 << IntNo);
383               if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for INT%d\n", attotime_to_double(timer_get_time(machine)), IntNo);
384               return;
385            }
386      }
387   }
388}
389
390/* Trigger an external interupt, optionally supplying the vector to take */
391static void external_int(running_machine *machine, UINT16 intno, UINT8 vector)
392{
393   if (LOG_INTERRUPTS_EXT) logerror("generating external int %02X, vector %02X\n",intno,vector);
394
395   // Only 4 external ints
396    if(intno>3)
397    {
398        logerror("external_int() invalid external interupt no : 0x%02X (can only be 0..3)\n",intno);
399        return;
400    }
401
402    // Only set external vector if cascade mode enabled, only valid for
403    // int 0 & int 1
404    if (intno<2)
405    {
406        if(i186.intr.ext[intno] & EXTINT_CTRL_CASCADE)
407            i186.intr.ext_vector[intno]=vector;
408    }
409
410    // Turn on the requested request bit and handle interrupt
411    i186.intr.request |= (0x010 << intno);
412    update_interrupt_state(machine);
413}
414
415
416/*************************************
417 *
418 *  80186 internal timers
419 *
420 *************************************/
421
422static TIMER_CALLBACK(internal_timer_int)
423{
424   int which = param;
425   struct timer_state *t = &i186.timer[which];
426
427   if (LOG_TIMER) logerror("Hit interrupt callback for timer %d\n", which);
428
429   /* set the max count bit */
430   t->control |= 0x0020;
431
432   /* request an interrupt */
433   if (t->control & 0x2000)
434   {
435      i186.intr.status |= 0x01 << which;
436      update_interrupt_state(machine);
437      if (LOG_TIMER) logerror("  Generating timer interrupt\n");
438   }
439
440   /* if we're continuous, reset */
441   if (t->control & 0x0001)
442   {
443      int count = t->maxA ? t->maxA : 0x10000;
444      timer_adjust_oneshot(t->int_timer, attotime_mul(ATTOTIME_IN_HZ(2000000), count), which);
445      if (LOG_TIMER) logerror("  Repriming interrupt\n");
446   }
447   else
448      timer_adjust_oneshot(t->int_timer, attotime_never, which);
449}
450
451
452static void internal_timer_sync(int which)
453{
454   struct timer_state *t = &i186.timer[which];
455
456   /* if we have a timing timer running, adjust the count */
457   if (t->time_timer_active)
458   {
459      attotime current_time = timer_timeelapsed(t->time_timer);
460      int net_clocks = attotime_mul(attotime_sub(current_time, t->last_time),  2000000).seconds;
461      t->last_time = current_time;
462
463      /* set the max count bit if we passed the max */
464      if ((int)t->count + net_clocks >= t->maxA)
465         t->control |= 0x0020;
466
467      /* set the new count */
468      if (t->maxA != 0)
469         t->count = (t->count + net_clocks) % t->maxA;
470      else
471         t->count = t->count + net_clocks;
472   }
473}
474
475
476static void internal_timer_update(running_machine *machine,
477                          int which,
478                                  int new_count,
479                                  int new_maxA,
480                                  int new_maxB,
481                                  int new_control)
482{
483   struct timer_state *t = &i186.timer[which];
484   int update_int_timer = 0;
485
486    logerror("internal_timer_update: %d, new_count=%d, new_maxA=%d, new_maxB=%d,new_control=%d",which,new_count,new_maxA,new_maxB,new_control);
487
488   /* if we have a new count and we're on, update things */
489   if (new_count != -1)
490   {
491      if (t->control & 0x8000)
492      {
493         internal_timer_sync(which);
494         update_int_timer = 1;
495      }
496      t->count = new_count;
497   }
498
499   /* if we have a new max and we're on, update things */
500   if (new_maxA != -1 && new_maxA != t->maxA)
501   {
502      if (t->control & 0x8000)
503      {
504         internal_timer_sync(which);
505         update_int_timer = 1;
506      }
507      t->maxA = new_maxA;
508      if (new_maxA == 0)
509      {
510               new_maxA = 0x10000;
511      }
512   }
513
514   /* if we have a new max and we're on, update things */
515   if (new_maxB != -1 && new_maxB != t->maxB)
516   {
517      if (t->control & 0x8000)
518      {
519         internal_timer_sync(which);
520         update_int_timer = 1;
521      }
522
523      t->maxB = new_maxB;
524
525      if (new_maxB == 0)
526      {
527               new_maxB = 0x10000;
528            }
529      }
530
531
532   /* handle control changes */
533   if (new_control != -1)
534   {
535      int diff;
536
537      /* merge back in the bits we don't modify */
538      new_control = (new_control & ~0x1fc0) | (t->control & 0x1fc0);
539
540      /* handle the /INH bit */
541      if (!(new_control & 0x4000))
542         new_control = (new_control & ~0x8000) | (t->control & 0x8000);
543      new_control &= ~0x4000;
544
545      /* check for control bits we don't handle */
546      diff = new_control ^ t->control;
547      if (diff & 0x001c)
548        logerror("%05X:ERROR! -unsupported timer mode %04X\n",
549            cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), new_control);
550
551      /* if we have real changes, update things */
552      if (diff != 0)
553      {
554
555         /* if we're going off, make sure our timers are gone */
556         if ((diff & 0x8000) && !(new_control & 0x8000))
557         {
558            /* compute the final count */
559            internal_timer_sync(which);
560
561            /* nuke the timer and force the interrupt timer to be recomputed */
562            timer_adjust_oneshot(t->time_timer, attotime_never, which);
563            t->time_timer_active = 0;
564            update_int_timer = 1;
565         }
566
567         /* if we're going on, start the timers running */
568         else if ((diff & 0x8000) && (new_control & 0x8000))
569         {
570            /* start the timing */
571            timer_adjust_oneshot(t->time_timer, attotime_never, which);
572            t->time_timer_active = 1;
573            update_int_timer = 1;
574         }
575
576         /* if something about the interrupt timer changed, force an update */
577         if (!(diff & 0x8000) && (diff & 0x2000))
578         {
579            internal_timer_sync(which);
580            update_int_timer = 1;
581         }
582      }
583
584      /* set the new control register */
585      t->control = new_control;
586   }
587
588   /* update the interrupt timer */
589      if (update_int_timer)
590      {
591            if ((t->control & 0x8000) && (t->control & 0x2000))
592            {
593              int diff = t->maxA - t->count;
594               if (diff <= 0)
595                  diff += 0x10000;
596               timer_adjust_oneshot(t->int_timer, attotime_mul(ATTOTIME_IN_HZ(2000000), diff), which);
597               if (LOG_TIMER) logerror("Set interrupt timer for %d\n", which);
598            }
599            else
600            {
601              timer_adjust_oneshot(t->int_timer, attotime_never, which);
602      }
603   }
604}
605
606
607
608/*************************************
609 *
610 *  80186 internal DMA
611 *
612 *************************************/
613
614static TIMER_CALLBACK(dma_timer_callback)
615{
616   int which = param;
617   struct dma_state *d = &i186.dma[which];
618
619   /* force an update and see if we're really done */
620   //stream_update(dma_stream, 0);
621
622   /* complete the status update */
623   d->control &= ~0x0002;
624   d->source += d->count;
625   d->count = 0;
626
627   /* check for interrupt generation */
628   if (d->control & 0x0100)
629   {
630      if (LOG_DMA) logerror("DMA%d timer callback - requesting interrupt: count = %04X, source = %04X\n", which, d->count, d->source);
631      i186.intr.request |= 0x04 << which;
632      update_interrupt_state(machine);
633   }
634}
635
636
637static void update_dma_control(running_machine *machine, int which, int new_control)
638{
639   struct dma_state *d = &i186.dma[which];
640   int diff;
641
642   /* handle the CHG bit */
643   if (!(new_control & CHG_NOCHG))
644     new_control = (new_control & ~ST_STOP) | (d->control & ST_STOP);
645   new_control &= ~CHG_NOCHG;
646
647   /* check for control bits we don't handle */
648   diff = new_control ^ d->control;
649   if (diff & 0x6811)
650     logerror("%05X:ERROR! - unsupported DMA mode %04X\n",
651         cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), new_control);
652
653   /* if we're going live, set a timer */
654   if ((diff & 0x0002) && (new_control & 0x0002))
655   {
656      /* make sure the parameters meet our expectations */
657      if ((new_control & 0xfe00) != 0x1600)
658      {
659         logerror("Unexpected DMA control %02X\n", new_control);
660      }
661      else if (/*!is_redline &&*/ ((d->dest & 1) || (d->dest & 0x3f) > 0x0b))
662      {
663         logerror("Unexpected DMA destination %02X\n", d->dest);
664      }
665      else if (/*is_redline && */ (d->dest & 0xf000) != 0x4000 && (d->dest & 0xf000) != 0x5000)
666      {
667         logerror("Unexpected DMA destination %02X\n", d->dest);
668      }
669
670      /* otherwise, set a timer */
671      else
672      {
673//          int count = d->count;
674
675         /* adjust for redline racer */
676            // int dacnum = (d->dest & 0x3f) / 2;
677
678         if (LOG_DMA) logerror("Initiated DMA %d - count = %04X, source = %04X, dest = %04X\n", which, d->count, d->source, d->dest);
679
680         d->finished = 0;
681/*          timer_adjust_oneshot(d->finish_timer,
682         ATTOTIME_IN_HZ(dac[dacnum].frequency) * (double)count, which);*/
683      }
684   }
685
686   /* set the new control register */
687   d->control = new_control;
688}
689
690static void drq_callback(running_machine *machine, int which)
691{
692    struct dma_state *dma = &i186.dma[which];
693   const address_space *memory_space   = cpu_get_address_space(cputag_get_cpu(machine, MAINCPU_TAG), ADDRESS_SPACE_PROGRAM);
694    const address_space *io_space       = cpu_get_address_space(cputag_get_cpu(machine, MAINCPU_TAG), ADDRESS_SPACE_IO);
695
696    const address_space *src_space;
697    const address_space *dest_space;
698
699    UINT16  dma_word;
700    UINT8   dma_byte;
701    UINT8   incdec_size;
702
703    if (LOG_DMA)
704        logerror("Control=%04X, src=%05X, dest=%05X, count=%04X\n",dma->control,dma->source,dma->dest,dma->count);
705
706    if(!(dma->control & ST_STOP))
707    {
708        logerror("%05X:ERROR! - drq%d with dma channel stopped\n",
709         cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), which);
710
711        return;
712    }
713
714    if(dma->control & DEST_MIO)
715    {
716        dest_space=memory_space;
717        //logerror("dest=memory\n");
718    }
719    else
720    {
721        dest_space=io_space;
722        //logerror("dest=I/O\n");
723    }
724
725    if(dma->control & SRC_MIO)
726    {
727        src_space=memory_space;
728        //logerror("src=memory\n");
729    }
730    else
731    {
732        src_space=io_space;
733        //logerror("src=I/O\n");
734    }
735
736    // Do the transfer
737    if(dma->control & BYTE_WORD)
738    {
739        //logerror("moving word\n");
740        dma_word=memory_read_word(src_space,dma->source);
741        memory_write_word(dest_space,dma->dest,dma_word);
742        incdec_size=2;
743    }
744    else
745    {
746        //logerror("moving byte\n");
747        dma_byte=memory_read_byte(src_space,dma->source);
748        memory_write_byte(dest_space,dma->dest,dma_byte);
749        incdec_size=1;
750    }
751
752    // Increment or Decrement destination ans source pointers as needed
753    //logerror("incrementing destination\n");
754    switch (dma->control & DEST_INCDEC_MASK)
755    {
756        case DEST_DECREMENT     : dma->dest -= incdec_size;
757        case DEST_INCREMENT     : dma->dest += incdec_size;
758    }
759
760    //logerror("incrementing source\n");
761    switch (dma->control & SRC_INCDEC_MASK)
762    {
763        case SRC_DECREMENT     : dma->source -= incdec_size;
764        case SRC_INCREMENT     : dma->source += incdec_size;
765    }
766
767    // decrement count
768    dma->count -= 1;
769
770    //logerror("Check term on zero\n");
771    // Terminate if count is zero, and terminate flag set
772    if((dma->control & TERMINATE_ON_ZERO) && (dma->count==0))
773    {
774        dma->control &= ~ST_STOP;
775        logerror("DMA terminated\n");
776    }
777
778    //logerror("Check int on zero\n");
779    // Interrupt if count is zero, and interrupt flag set
780    if((dma->control & INTERRUPT_ON_ZERO) && (dma->count==0))
781    {
782      if (LOG_DMA) logerror("DMA%d - requesting interrupt: count = %04X, source = %04X\n", which, dma->count, dma->source);
783      i186.intr.request |= 0x04 << which;
784      update_interrupt_state(machine);
785    }
786    //logerror("DMA callback done\n");
787}
788
789/*-------------------------------------------------------------------------*/
790/* Name: rmnimbus                                                            */
791/* Desc: CPU - Initialize the 80186 CPU                                    */
792/*-------------------------------------------------------------------------*/
793static void nimbus_cpu_init(running_machine *machine)
794{
795
796    logerror("Machine reset\n");
797
798   /* create timers here so they stick around */
799   i186.timer[0].int_timer = timer_alloc(machine, internal_timer_int, NULL);
800   i186.timer[1].int_timer = timer_alloc(machine, internal_timer_int, NULL);
801   i186.timer[2].int_timer = timer_alloc(machine, internal_timer_int, NULL);
802   i186.timer[0].time_timer = timer_alloc(machine, NULL, NULL);
803   i186.timer[1].time_timer = timer_alloc(machine, NULL, NULL);
804   i186.timer[2].time_timer = timer_alloc(machine, NULL, NULL);
805   i186.dma[0].finish_timer = timer_alloc(machine, dma_timer_callback, NULL);
806   i186.dma[1].finish_timer = timer_alloc(machine, dma_timer_callback, NULL);
807}
808
809static void nimbus_cpu_reset(running_machine *machine)
810{
811      /* reset the interrupt state */
812   i186.intr.priority_mask       = 0x0007;
813   i186.intr.timer          = 0x000f;
814   i186.intr.dma[0]         = 0x000f;
815   i186.intr.dma[1]         = 0x000f;
816   i186.intr.ext[0]         = 0x000f;
817   i186.intr.ext[1]         = 0x000f;
818   i186.intr.ext[2]         = 0x000f;
819   i186.intr.ext[3]         = 0x000f;
820    i186.intr.in_service        = 0x0000;
821
822    /* External vectors by default to internal int 0/1 vectors */
823    i186.intr.ext_vector[0]      = 0x000C;
824   i186.intr.ext_vector[1]      = 0x000D;
825
826    i186.intr.pending           = 0x0000;
827   i186.intr.ack_mask          = 0x0000;
828   i186.intr.request           = 0x0000;
829   i186.intr.status            = 0x0000;
830   i186.intr.poll_status       = 0x0000;
831
832    logerror("CPU reset done\n");
833}
834
835READ16_HANDLER( i186_internal_port_r )
836{
837   int temp, which;
838
839   switch (offset)
840   {
841      case 0x11:
842         logerror("%05X:ERROR - read from 80186 EOI\n", cpu_get_pc(space->cpu));
843         break;
844
845      case 0x12:
846         if (LOG_PORTS) logerror("%05X:read 80186 interrupt poll\n", cpu_get_pc(space->cpu));
847         if (i186.intr.poll_status & 0x8000)
848            int_callback(cputag_get_cpu(space->machine, MAINCPU_TAG), 0);
849         return i186.intr.poll_status;
850
851      case 0x13:
852         if (LOG_PORTS) logerror("%05X:read 80186 interrupt poll status\n", cpu_get_pc(space->cpu));
853         return i186.intr.poll_status;
854
855      case 0x14:
856         if (LOG_PORTS) logerror("%05X:read 80186 interrupt mask\n", cpu_get_pc(space->cpu));
857         temp  = (i186.intr.timer  >> 3) & 0x01;
858         temp |= (i186.intr.dma[0] >> 1) & 0x04;
859         temp |= (i186.intr.dma[1] >> 0) & 0x08;
860         temp |= (i186.intr.ext[0] << 1) & 0x10;
861         temp |= (i186.intr.ext[1] << 2) & 0x20;
862         temp |= (i186.intr.ext[2] << 3) & 0x40;
863         temp |= (i186.intr.ext[3] << 4) & 0x80;
864         return temp;
865
866      case 0x15:
867         if (LOG_PORTS) logerror("%05X:read 80186 interrupt priority mask\n", cpu_get_pc(space->cpu));
868         return i186.intr.priority_mask;
869
870      case 0x16:
871         if (LOG_PORTS) logerror("%05X:read 80186 interrupt in-service\n", cpu_get_pc(space->cpu));
872         return i186.intr.in_service;
873
874      case 0x17:
875         if (LOG_PORTS) logerror("%05X:read 80186 interrupt request\n", cpu_get_pc(space->cpu));
876         temp = i186.intr.request & ~0x0001;
877         if (i186.intr.status & 0x0007)
878            temp |= 1;
879         return temp;
880
881      case 0x18:
882         if (LOG_PORTS) logerror("%05X:read 80186 interrupt status\n", cpu_get_pc(space->cpu));
883         return i186.intr.status;
884
885      case 0x19:
886         if (LOG_PORTS) logerror("%05X:read 80186 timer interrupt control\n", cpu_get_pc(space->cpu));
887         return i186.intr.timer;
888
889      case 0x1a:
890         if (LOG_PORTS) logerror("%05X:read 80186 DMA 0 interrupt control\n", cpu_get_pc(space->cpu));
891         return i186.intr.dma[0];
892
893      case 0x1b:
894         if (LOG_PORTS) logerror("%05X:read 80186 DMA 1 interrupt control\n", cpu_get_pc(space->cpu));
895         return i186.intr.dma[1];
896
897      case 0x1c:
898         if (LOG_PORTS) logerror("%05X:read 80186 INT 0 interrupt control\n", cpu_get_pc(space->cpu));
899         return i186.intr.ext[0];
900
901      case 0x1d:
902         if (LOG_PORTS) logerror("%05X:read 80186 INT 1 interrupt control\n", cpu_get_pc(space->cpu));
903         return i186.intr.ext[1];
904
905      case 0x1e:
906         if (LOG_PORTS) logerror("%05X:read 80186 INT 2 interrupt control\n", cpu_get_pc(space->cpu));
907         return i186.intr.ext[2];
908
909      case 0x1f:
910         if (LOG_PORTS) logerror("%05X:read 80186 INT 3 interrupt control\n", cpu_get_pc(space->cpu));
911         return i186.intr.ext[3];
912
913      case 0x28:
914      case 0x2c:
915      case 0x30:
916         if (LOG_PORTS) logerror("%05X:read 80186 Timer %d count\n", cpu_get_pc(space->cpu), (offset - 0x28) / 4);
917         which = (offset - 0x28) / 4;
918         if (!(offset & 1))
919            internal_timer_sync(which);
920         return i186.timer[which].count;
921
922      case 0x29:
923      case 0x2d:
924      case 0x31:
925         if (LOG_PORTS) logerror("%05X:read 80186 Timer %d max A\n", cpu_get_pc(space->cpu), (offset - 0x29) / 4);
926         which = (offset - 0x29) / 4;
927         return i186.timer[which].maxA;
928
929      case 0x2a:
930      case 0x2e:
931         logerror("%05X:read 80186 Timer %d max B\n", cpu_get_pc(space->cpu), (offset - 0x2a) / 4);
932         which = (offset - 0x2a) / 4;
933         return i186.timer[which].maxB;
934
935      case 0x2b:
936      case 0x2f:
937      case 0x33:
938         if (LOG_PORTS) logerror("%05X:read 80186 Timer %d control\n", cpu_get_pc(space->cpu), (offset - 0x2b) / 4);
939         which = (offset - 0x2b) / 4;
940         return i186.timer[which].control;
941
942      case 0x50:
943         if (LOG_PORTS) logerror("%05X:read 80186 upper chip select\n", cpu_get_pc(space->cpu));
944         return i186.mem.upper;
945
946      case 0x51:
947         if (LOG_PORTS) logerror("%05X:read 80186 lower chip select\n", cpu_get_pc(space->cpu));
948         return i186.mem.lower;
949
950      case 0x52:
951         if (LOG_PORTS) logerror("%05X:read 80186 peripheral chip select\n", cpu_get_pc(space->cpu));
952         return i186.mem.peripheral;
953
954      case 0x53:
955         if (LOG_PORTS) logerror("%05X:read 80186 middle chip select\n", cpu_get_pc(space->cpu));
956         return i186.mem.middle;
957
958      case 0x54:
959         if (LOG_PORTS) logerror("%05X:read 80186 middle P chip select\n", cpu_get_pc(space->cpu));
960         return i186.mem.middle_size;
961
962      case 0x60:
963      case 0x68:
964         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d lower source address\n", cpu_get_pc(space->cpu), (offset - 0x60) / 8);
965         which = (offset - 0x60) / 8;
966//          stream_update(dma_stream, 0);
967         return i186.dma[which].source;
968
969      case 0x61:
970      case 0x69:
971         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d upper source address\n", cpu_get_pc(space->cpu), (offset - 0x61) / 8);
972         which = (offset - 0x61) / 8;
973//          stream_update(dma_stream, 0);
974         return i186.dma[which].source >> 16;
975
976      case 0x62:
977      case 0x6a:
978         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d lower dest address\n", cpu_get_pc(space->cpu), (offset - 0x62) / 8);
979         which = (offset - 0x62) / 8;
980//          stream_update(dma_stream, 0);
981         return i186.dma[which].dest;
982
983      case 0x63:
984      case 0x6b:
985         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d upper dest address\n", cpu_get_pc(space->cpu), (offset - 0x63) / 8);
986         which = (offset - 0x63) / 8;
987//          stream_update(dma_stream, 0);
988         return i186.dma[which].dest >> 16;
989
990      case 0x64:
991      case 0x6c:
992         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d transfer count\n", cpu_get_pc(space->cpu), (offset - 0x64) / 8);
993         which = (offset - 0x64) / 8;
994//          stream_update(dma_stream, 0);
995         return i186.dma[which].count;
996
997      case 0x65:
998      case 0x6d:
999         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d control\n", cpu_get_pc(space->cpu), (offset - 0x65) / 8);
1000         which = (offset - 0x65) / 8;
1001//          stream_update(dma_stream, 0);
1002         return i186.dma[which].control;
1003
1004      default:
1005         logerror("%05X:read 80186 port %02X\n", cpu_get_pc(space->cpu), offset);
1006         break;
1007   }
1008   return 0x00;
1009}
1010
1011/*************************************
1012 *
1013 *  80186 internal I/O writes
1014 *
1015 *************************************/
1016
1017WRITE16_HANDLER( i186_internal_port_w )
1018{
1019   int temp, which, data16 = data;
1020
1021   switch (offset)
1022   {
1023      case 0x11:
1024         if (LOG_PORTS) logerror("%05X:80186 EOI = %04X\n", cpu_get_pc(space->cpu), data16);
1025         handle_eoi(space->machine,0x8000);
1026         update_interrupt_state(space->machine);
1027         break;
1028
1029      case 0x12:
1030         logerror("%05X:ERROR - write to 80186 interrupt poll = %04X\n", cpu_get_pc(space->cpu), data16);
1031         break;
1032
1033      case 0x13:
1034         logerror("%05X:ERROR - write to 80186 interrupt poll status = %04X\n", cpu_get_pc(space->cpu), data16);
1035         break;
1036
1037      case 0x14:
1038         if (LOG_PORTS) logerror("%05X:80186 interrupt mask = %04X\n", cpu_get_pc(space->cpu), data16);
1039         i186.intr.timer  = (i186.intr.timer  & ~0x08) | ((data16 << 3) & 0x08);
1040         i186.intr.dma[0] = (i186.intr.dma[0] & ~0x08) | ((data16 << 1) & 0x08);
1041         i186.intr.dma[1] = (i186.intr.dma[1] & ~0x08) | ((data16 << 0) & 0x08);
1042         i186.intr.ext[0] = (i186.intr.ext[0] & ~0x08) | ((data16 >> 1) & 0x08);
1043         i186.intr.ext[1] = (i186.intr.ext[1] & ~0x08) | ((data16 >> 2) & 0x08);
1044         i186.intr.ext[2] = (i186.intr.ext[2] & ~0x08) | ((data16 >> 3) & 0x08);
1045         i186.intr.ext[3] = (i186.intr.ext[3] & ~0x08) | ((data16 >> 4) & 0x08);
1046         update_interrupt_state(space->machine);
1047         break;
1048
1049      case 0x15:
1050         if (LOG_PORTS) logerror("%05X:80186 interrupt priority mask = %04X\n", cpu_get_pc(space->cpu), data16);
1051         i186.intr.priority_mask = data16 & 0x0007;
1052         update_interrupt_state(space->machine);
1053         break;
1054
1055      case 0x16:
1056         if (LOG_PORTS) logerror("%05X:80186 interrupt in-service = %04X\n", cpu_get_pc(space->cpu), data16);
1057         i186.intr.in_service = data16 & 0x00ff;
1058         update_interrupt_state(space->machine);
1059         break;
1060
1061      case 0x17:
1062         if (LOG_PORTS) logerror("%05X:80186 interrupt request = %04X\n", cpu_get_pc(space->cpu), data16);
1063         i186.intr.request = (i186.intr.request & ~0x00c0) | (data16 & 0x00c0);
1064         update_interrupt_state(space->machine);
1065         break;
1066
1067      case 0x18:
1068         if (LOG_PORTS) logerror("%05X:WARNING - wrote to 80186 interrupt status = %04X\n", cpu_get_pc(space->cpu), data16);
1069         i186.intr.status = (i186.intr.status & ~0x8007) | (data16 & 0x8007);
1070         update_interrupt_state(space->machine);
1071         break;
1072
1073      case 0x19:
1074         if (LOG_PORTS) logerror("%05X:80186 timer interrupt contol = %04X\n", cpu_get_pc(space->cpu), data16);
1075         i186.intr.timer = data16 & 0x000f;
1076         break;
1077
1078      case 0x1a:
1079         if (LOG_PORTS) logerror("%05X:80186 DMA 0 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1080         i186.intr.dma[0] = data16 & 0x000f;
1081         break;
1082
1083      case 0x1b:
1084         if (LOG_PORTS) logerror("%05X:80186 DMA 1 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1085         i186.intr.dma[1] = data16 & 0x000f;
1086         break;
1087
1088      case 0x1c:
1089         if (LOG_PORTS) logerror("%05X:80186 INT 0 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1090         i186.intr.ext[0] = data16 & 0x007f;
1091         break;
1092
1093      case 0x1d:
1094         if (LOG_PORTS) logerror("%05X:80186 INT 1 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1095         i186.intr.ext[1] = data16 & 0x007f;
1096         break;
1097
1098      case 0x1e:
1099         if (LOG_PORTS) logerror("%05X:80186 INT 2 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1100         i186.intr.ext[2] = data16 & 0x001f;
1101         break;
1102
1103      case 0x1f:
1104         if (LOG_PORTS) logerror("%05X:80186 INT 3 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1105         i186.intr.ext[3] = data16 & 0x001f;
1106         break;
1107
1108      case 0x28:
1109      case 0x2c:
1110      case 0x30:
1111         if (LOG_PORTS) logerror("%05X:80186 Timer %d count = %04X\n", cpu_get_pc(space->cpu), (offset - 0x28) / 4, data16);
1112         which = (offset - 0x28) / 4;
1113         internal_timer_update(space->machine,which, data16, -1, -1, -1);
1114         break;
1115
1116      case 0x29:
1117      case 0x2d:
1118      case 0x31:
1119         if (LOG_PORTS) logerror("%05X:80186 Timer %d max A = %04X\n", cpu_get_pc(space->cpu), (offset - 0x29) / 4, data16);
1120         which = (offset - 0x29) / 4;
1121         internal_timer_update(space->machine,which, -1, data16, -1, -1);
1122         break;
1123
1124      case 0x2a:
1125      case 0x2e:
1126         if (LOG_PORTS) logerror("%05X:80186 Timer %d max B = %04X\n", cpu_get_pc(space->cpu), (offset - 0x2a) / 4, data16);
1127         which = (offset - 0x2a) / 4;
1128         internal_timer_update(space->machine,which, -1, -1, data16, -1);
1129         break;
1130
1131      case 0x2b:
1132      case 0x2f:
1133      case 0x33:
1134         if (LOG_PORTS) logerror("%05X:80186 Timer %d control = %04X\n", cpu_get_pc(space->cpu), (offset - 0x2b) / 4, data16);
1135         which = (offset - 0x2b) / 4;
1136         internal_timer_update(space->machine,which, -1, -1, -1, data16);
1137         break;
1138
1139      case 0x50:
1140         if (LOG_PORTS) logerror("%05X:80186 upper chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1141         i186.mem.upper = data16 | 0xc038;
1142         break;
1143
1144      case 0x51:
1145         if (LOG_PORTS) logerror("%05X:80186 lower chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1146         i186.mem.lower = (data16 & 0x3fff) | 0x0038; printf("%X",i186.mem.lower);
1147         break;
1148
1149      case 0x52:
1150         if (LOG_PORTS) logerror("%05X:80186 peripheral chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1151         i186.mem.peripheral = data16 | 0x0038;
1152         break;
1153
1154      case 0x53:
1155         if (LOG_PORTS) logerror("%05X:80186 middle chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1156         i186.mem.middle = data16 | 0x01f8;
1157         break;
1158
1159      case 0x54:
1160         if (LOG_PORTS) logerror("%05X:80186 middle P chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1161         i186.mem.middle_size = data16 | 0x8038;
1162
1163         temp = (i186.mem.peripheral & 0xffc0) << 4;
1164         if (i186.mem.middle_size & 0x0040)
1165         {
1166//              install_mem_read_handler(2, temp, temp + 0x2ff, peripheral_r);
1167//              install_mem_write_handler(2, temp, temp + 0x2ff, peripheral_w);
1168         }
1169         else
1170         {
1171            temp &= 0xffff;
1172//              install_port_read_handler(2, temp, temp + 0x2ff, peripheral_r);
1173//              install_port_write_handler(2, temp, temp + 0x2ff, peripheral_w);
1174         }
1175
1176         /* we need to do this at a time when the I86 context is swapped in */
1177         /* this register is generally set once at startup and never again, so it's a good */
1178         /* time to set it up */
1179         cpu_set_irq_callback(space->cpu, int_callback);
1180         break;
1181
1182      case 0x60:
1183      case 0x68:
1184         if (LOG_PORTS) logerror("%05X:80186 DMA%d lower source address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x60) / 8, data16);
1185         which = (offset - 0x60) / 8;
1186//          stream_update(dma_stream, 0);
1187         i186.dma[which].source = (i186.dma[which].source & ~0x0ffff) | (data16 & 0x0ffff);
1188         break;
1189
1190      case 0x61:
1191      case 0x69:
1192         if (LOG_PORTS) logerror("%05X:80186 DMA%d upper source address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x61) / 8, data16);
1193         which = (offset - 0x61) / 8;
1194//          stream_update(dma_stream, 0);
1195         i186.dma[which].source = (i186.dma[which].source & ~0xf0000) | ((data16 << 16) & 0xf0000);
1196         break;
1197
1198      case 0x62:
1199      case 0x6a:
1200         if (LOG_PORTS) logerror("%05X:80186 DMA%d lower dest address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x62) / 8, data16);
1201         which = (offset - 0x62) / 8;
1202//          stream_update(dma_stream, 0);
1203         i186.dma[which].dest = (i186.dma[which].dest & ~0x0ffff) | (data16 & 0x0ffff);
1204         break;
1205
1206      case 0x63:
1207      case 0x6b:
1208         if (LOG_PORTS) logerror("%05X:80186 DMA%d upper dest address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x63) / 8, data16);
1209         which = (offset - 0x63) / 8;
1210//          stream_update(dma_stream, 0);
1211         i186.dma[which].dest = (i186.dma[which].dest & ~0xf0000) | ((data16 << 16) & 0xf0000);
1212         break;
1213
1214      case 0x64:
1215      case 0x6c:
1216         if (LOG_PORTS) logerror("%05X:80186 DMA%d transfer count = %04X\n", cpu_get_pc(space->cpu), (offset - 0x64) / 8, data16);
1217         which = (offset - 0x64) / 8;
1218//          stream_update(dma_stream, 0);
1219         i186.dma[which].count = data16;
1220         break;
1221
1222      case 0x65:
1223      case 0x6d:
1224         if (LOG_PORTS) logerror("%05X:80186 DMA%d control = %04X\n", cpu_get_pc(space->cpu), (offset - 0x65) / 8, data16);
1225         which = (offset - 0x65) / 8;
1226//          stream_update(dma_stream, 0);
1227         update_dma_control(space->machine, which, data16);
1228         break;
1229
1230      case 0x7f:
1231         if (LOG_PORTS) logerror("%05X:80186 relocation register = %04X\n", cpu_get_pc(space->cpu), data16);
1232
1233         /* we assume here there that this doesn't happen too often */
1234         /* plus, we can't really remove the old memory range, so we also assume that it's */
1235         /* okay to leave us mapped where we were */
1236         temp = (data16 & 0x0fff) << 8;
1237         if (data16 & 0x1000)
1238         {
1239            memory_install_read16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_PROGRAM), temp, temp + 0xff, 0, 0, i186_internal_port_r);
1240            memory_install_write16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_PROGRAM), temp, temp + 0xff, 0, 0, i186_internal_port_w);
1241         }
1242         else
1243         {
1244            temp &= 0xffff;
1245            memory_install_read16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_IO), temp, temp + 0xff, 0, 0, i186_internal_port_r);
1246            memory_install_write16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_IO), temp, temp + 0xff, 0, 0, i186_internal_port_w);
1247         }
1248/*          popmessage("Sound CPU reset");*/
1249         break;
1250
1251      default:
1252         logerror("%05X:80186 port %02X = %04X\n", cpu_get_pc(space->cpu), offset, data16);
1253         break;
1254   }
1255}
1256
1257MACHINE_RESET(nimbus)
1258{
1259   /* CPU */
1260   nimbus_cpu_reset(machine);
1261    fdc_reset();
1262    keyboard_reset();
1263}
1264
1265DRIVER_INIT(nimbus)
1266{
1267}
1268
1269MACHINE_START( nimbus )
1270{
1271    /* init cpu */
1272    nimbus_cpu_init(machine);
1273
1274    keyboard.keyscan_timer=timer_alloc(machine, keyscan_callback, NULL);
1275
1276   /* setup debug commands */
1277   if (machine->debug_flags & DEBUG_FLAG_ENABLED)
1278   {
1279      debug_console_register_command(machine, "nimbus_irq", CMDFLAG_NONE, 0, 0, 1, execute_debug_irq);
1280        debug_console_register_command(machine, "nimbus_intmasks", CMDFLAG_NONE, 0, 0, 0, execute_debug_intmasks);
1281
1282        /* set up the instruction hook */
1283        debug_cpu_set_instruction_hook(cputag_get_cpu(machine, MAINCPU_TAG), instruction_hook);
1284
1285    }
1286}
1287
1288static void execute_debug_irq(running_machine *machine, int ref, int params, const char *param[])
1289{
1290    int IntNo;
1291    int IntMask;
1292
1293    if(params>0)
1294    {
1295        sscanf(param[0],"%d",&IntNo);
1296
1297        IntMask=0x0000;
1298
1299        switch (IntNo)
1300        {
1301            case 0  : IntMask=0x010; break;
1302            case 1  : IntMask=0x020; break;
1303            case 2  : IntMask=0x040; break;
1304            case 3  : IntMask=0x080; break;
1305        }
1306
1307        i186.intr.request |= IntMask;
1308        update_interrupt_state(machine);
1309
1310        debug_console_printf(machine,"triggering IRQ%d, IntMask=%4.4X, i186.intr.request=%4.4X\n",IntNo,IntMask,i186.intr.request);
1311
1312//        cputag_set_input_line(machine, MAINCPU_TAG, IntNo, HOLD_LINE );
1313//        cputag_set_input_line(machine, MAINCPU_TAG, IntNo, CLEAR_LINE );
1314
1315    }
1316    else
1317    {
1318        debug_console_printf(machine,"Error, you must supply an intno to trigger\n");
1319    }
1320
1321
1322}
1323
1324
1325static void execute_debug_intmasks(running_machine *machine, int ref, int params, const char *param[])
1326{
1327    int IntNo;
1328
1329
1330    debug_console_printf(machine,"i186.intr.priority_mask=%4X\n",i186.intr.priority_mask);
1331    for(IntNo=0; IntNo<4; IntNo++)
1332    {
1333        debug_console_printf(machine,"extInt%d mask=%4X\n",IntNo,i186.intr.ext[IntNo]);
1334    }
1335
1336    debug_console_printf(machine,"i186.intr.request   = %04X\n",i186.intr.request);
1337    debug_console_printf(machine,"i186.intr.ack_mask  = %04X\n",i186.intr.ack_mask);
1338    debug_console_printf(machine,"i186.intr.in_service= %04X\n",i186.intr.in_service);
1339}
1340
1341/*-----------------------------------------------
1342    instruction_hook - per-instruction hook
1343-----------------------------------------------*/
1344
1345static int instruction_hook(const device_config *device, offs_t curpc)
1346{
1347    const address_space *space = cpu_get_address_space(device, ADDRESS_SPACE_PROGRAM);
1348    UINT8               *addr_ptr;
1349
1350    addr_ptr = memory_get_read_ptr(space,curpc);
1351
1352    if ((addr_ptr !=NULL) && (addr_ptr[0]==0xCD) && (addr_ptr[1]==0xF0))
1353        decode_subbios(device,curpc);
1354
1355    return 0;
1356}
1357
1358#define set_type(type_name)     sprintf(type_str,type_name)
1359#define set_drv(drv_name)       sprintf(drv_str,drv_name)
1360#define set_func(func_name)     sprintf(func_str,func_name)
1361
1362static void decode_subbios(const device_config *device,offs_t pc)
1363{
1364    char    type_str[80];
1365    char    drv_str[80];
1366    char    func_str[80];
1367
1368    void (*dump_dssi)(const device_config *,UINT16, UINT16) = NULL;
1369
1370    const device_config *cpu = cputag_get_cpu(device->machine,MAINCPU_TAG);
1371
1372    UINT16  ax = cpu_get_reg(cpu,I8086_AX);
1373    UINT16  bx = cpu_get_reg(cpu,I8086_BX);
1374    UINT16  cx = cpu_get_reg(cpu,I8086_CX);
1375    UINT16  ds = cpu_get_reg(cpu,I8086_DS);
1376    UINT16  si = cpu_get_reg(cpu,I8086_SI);
1377
1378    logerror("=======================================================================\n");
1379    logerror("Sub-bios call at %08X, AX=%04X, BX=%04X, CX=%04X, DS:SI=%04X:%04X\n",pc,ax,bx,cx,ds,si);
1380
1381    set_type("invalid");
1382    set_drv("invalid");
1383    set_func("invalid");
1384
1385
1386    switch (cx)
1387    {
1388        case 0   :
1389        {
1390            set_type("t_mummu");
1391            set_drv("d_mummu");
1392
1393            switch (ax)
1394            {
1395                case 0  : set_func("f_get_version_number"); break;
1396                case 1  : set_func("f_add_type_code"); break;
1397                case 2  : set_func("f_del_typc_code"); break;
1398                case 3  : set_func("f_get_TCB"); break;
1399                case 4  : set_func("f_add_driver_cdoe"); break;
1400                case 5  : set_func("f_del_driver_code"); break;
1401                case 6  : set_func("f_get_DCB"); break;
1402                case 7  : set_func("f_get_copyright"); break;
1403            }
1404        }; break;
1405
1406        case 1   :
1407        {
1408            set_type("t_character");
1409            set_drv("d_printer");
1410
1411            switch(ax)
1412            {
1413                case 0  : set_func("f_get_version_number"); break;
1414                case 1  : set_func("f_get_output_status"); break;
1415                case 2  : set_func("f_output_character"); break;
1416                case 3  : set_func("f_get_input_status"); break;
1417                case 4  : set_func("f_get_and_remove"); break;
1418                case 5  : set_func("f_get_no_remove"); break;
1419                case 6  : set_func("f_get_last_and_remove"); break;
1420                case 7  : set_func("f_get_last_no_remove"); break;
1421                case 8  : set_func("f_set_IO_parameters"); break;
1422            }
1423        }; break;
1424
1425        case 2   :
1426        {
1427            set_type("t_disk");
1428
1429            switch(bx)
1430            {
1431                case 0  : set_drv("d_floppy"); break;
1432                case 1  : set_drv("d_winchester"); break;
1433                case 2  : set_drv("d_tape"); break;
1434                case 3  : set_drv("d_rompack"); break;
1435                case 4  : set_drv("d_eeprom"); break;
1436            }
1437
1438            switch(ax)
1439            {
1440                case 0  : set_func("f_get_version_number"); break;
1441                case 1  : set_func("f_initialise_unit"); break;
1442                case 2  : set_func("f_pseudo_init_unit"); break;
1443                case 3  : set_func("f_get_device_status"); break;
1444                case 4  : set_func("f_read_n_sectors"); break;
1445                case 5  : set_func("f_write_n_sectors"); break;
1446                case 6  : set_func("f_verify_n_sectors"); break;
1447                case 7  : set_func("f_media_check"); break;
1448                case 8  : set_func("f_recalibrate"); break;
1449                case 9  : set_func("f_motors_off"); break;
1450            }
1451
1452        }; break;
1453
1454        case 3   :
1455        {
1456            set_type("t_piconet");
1457            switch(ax)
1458            {
1459                case 0  : set_func("f_get_version_number"); break;
1460            }
1461        }; break;
1462
1463        case 4   :
1464        {
1465            set_type("t_tick");
1466            set_drv("d_tick");
1467
1468            switch(ax)
1469            {
1470                case 0  : set_func("f_get_version_number"); break;
1471                case 1  : set_func("f_ticks_per_second"); break;
1472                case 2  : set_func("f_link_tick_routine"); break;
1473                case 3  : set_func("f_unlink_tick_routine"); break;
1474            }
1475        }; break;
1476
1477        case 5   :
1478        {
1479            set_type("t_graphics_input");
1480            switch(ax)
1481            {
1482                case 0  : set_func("f_get_version_number"); break;
1483            }
1484        }; break;
1485
1486        case 6   :
1487        {
1488            set_type("t_graphics_output");
1489            set_drv("d_ngc_screen");
1490
1491            switch(ax)
1492            {
1493                case 0  : set_func("f_get_version_number");                 break;
1494                case 1  : set_func("f_graphics_output_cold_start");         break;
1495                case 2  : set_func("f_graphics_output_warm_start");         break;
1496                case 3  : set_func("f_graphics_output_off");                break;
1497                case 4  : set_func("f_reinit_graphics_output");             break;
1498                case 5  : set_func("f_polymarker");                         break;
1499                case 6  : set_func("f_polyline");                           break;
1500                case 7  : set_func("f_fill_area"); dump_dssi=&decode_dssi_f_fill_area; break;
1501                case 8  : set_func("f_flood_fill_area"); break;
1502                case 9  : set_func("f_plot_character_string"); dump_dssi=&decode_dssi_f_plot_character_string; break;
1503                case 10 : set_func("f_define_graphics_clipping_area"); break;
1504                case 11 : set_func("f_enquire_clipping_area_limits"); break;
1505                case 12 : set_func("f_select_graphics_clipping_area"); break;
1506                case 13 : set_func("f_enq_selctd_graphics_clip_area"); break;
1507                case 14 : set_func("f_set_clt_element"); break;
1508                case 15 : set_func("f_enquire_clt_element"); break;
1509                case 16 : set_func("f_set_new_clt"); dump_dssi=&decode_dssi_f_set_new_clt; break;
1510                case 17 : set_func("f_enquire_clt_contents"); break;
1511                case 18 : set_func("f_define_dithering_pattern"); break;
1512                case 19 : set_func("f_enquire_dithering_pattern"); break;
1513                case 20 : set_func("f_draw_sprite"); break;
1514                case 21 : set_func("f_move_sprite"); break;
1515                case 22 : set_func("f_erase_sprite"); break;
1516                case 23 : set_func("f_read_pixel"); break;
1517                case 24 : set_func("f_read_to_limit"); break;
1518                case 25 : set_func("f_read_area_pixel"); break;
1519                case 26 : set_func("f_write_area_pixel"); break;
1520                case 27 : set_func("f_copy_area_pixel"); break;
1521
1522                case 29 : set_func("f_read_area_word"); break;
1523                case 30 : set_func("f_write_area_word"); break;
1524                case 31 : set_func("f_copy_area_word"); break;
1525                case 32 : set_func("f_swap_area_word"); break;
1526                case 33 : set_func("f_set_border_colour"); break;
1527                case 34 : set_func("f_enquire_border_colour"); break;
1528                case 35 : set_func("f_enquire_miscellaneous_data"); break;
1529                case 36  : set_func("f_circle"); break;
1530
1531                case 38 : set_func("f_arc_of_ellipse"); break;
1532                case 39 : set_func("f_isin"); break;
1533                case 40 : set_func("f_icos"); break;
1534                case 41 : set_func("f_define_hatching_pattern"); break;
1535                case 42 : set_func("f_enquire_hatching_pattern"); break;
1536                case 43 : set_func("f_enquire_display_line"); break;
1537                case 44 : set_func("f_plonk_logo"); break;
1538            }
1539        }; break;
1540
1541        case 7   :
1542        {
1543            set_type("t_zend");
1544            switch(ax)
1545            {
1546                case 0  : set_func("f_get_version_number"); break;
1547            }
1548        }; break;
1549
1550        case 8   :
1551        {
1552            set_type("t_zep");
1553            switch(ax)
1554            {
1555                case 0  : set_func("f_get_version_number"); break;
1556            }
1557        }; break;
1558
1559        case 9   :
1560        {
1561            set_type("t_raw_console");
1562
1563            switch(bx)
1564            {
1565                case 0  :
1566                {
1567                    set_drv("d_screen");
1568
1569                    switch(ax)
1570                    {
1571                        case 0  : set_func("f_get_version_number"); break;
1572                        case 1  : set_func("f_plonk_char"); break;
1573                        case 2  : set_func("f_plonk_cursor"); break;
1574                        case 3  : set_func("f_kill_cursor"); break;
1575                        case 4  : set_func("f_scroll"); break;
1576                        case 5  : set_func("f_width"); break;
1577                        case 6  : set_func("f_get_char_set"); break;
1578                        case 7  : set_func("f_set_char_set"); break;
1579                        case 8  : set_func("f_reset_char_set"); break;
1580                        case 9  : set_func("f_set_plonk_parameters"); break;
1581                        case 10 : set_func("f_set_cursor_flash_rate"); break;
1582                    }
1583                }; break;
1584
1585                case 1  :
1586                {
1587                    set_drv("d_keyboard");
1588
1589                    switch(ax)
1590                    {
1591                        case 0  : set_func("f_get_version_number"); break;
1592                        case 1  : set_func("f_init_keyboard"); break;
1593                        case 2  : set_func("f_get_last_key_code"); break;
1594                        case 3  : set_func("f_get_bitmap"); break;
1595                    }
1596                }; break;
1597            }
1598        }; break;
1599
1600        case 10   :
1601        {
1602            set_type("t_acoustics");
1603            switch(ax)
1604            {
1605                case 0  : set_func("f_get_version_number"); break;
1606            }
1607        }; break;
1608
1609        case 11   :
1610        {
1611            set_type("t_hard_sums");
1612            switch(ax)
1613            {
1614                case 0  : set_func("f_get_version_number"); break;
1615            }
1616        }; break;
1617    }
1618
1619    logerror("Type=%s, Driver=%s, Function=%s\n",type_str,drv_str,func_str);
1620
1621    if(dump_dssi!=NULL)
1622        dump_dssi(device,ds,si);
1623    logerror("=======================================================================\n");
1624}
1625
1626void *get_dssi_ptr(const address_space *space, UINT16   ds, UINT16 si)
1627{
1628    int             addr;
1629
1630    addr=((ds<<4)+si);
1631    OUTPUT_SEGOFS("DS:SI",ds,si);
1632
1633    return memory_get_read_ptr(space, addr);
1634}
1635
1636static void decode_dssi_f_fill_area(const device_config *device,UINT16  ds, UINT16 si)
1637{
1638    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1639
1640    UINT16          *addr_ptr;
1641    t_area_params   *area_params;
1642    t_nimbus_brush  *brush;
1643    int             cocount;
1644
1645    area_params = get_dssi_ptr(space,ds,si);
1646
1647    OUTPUT_SEGOFS("SegBrush:OfsBrush",area_params->seg_brush,area_params->ofs_brush);
1648    brush=memory_get_read_ptr(space, LINEAR_ADDR(area_params->seg_brush,area_params->ofs_brush));
1649
1650    logerror("Brush params\n");
1651    logerror("Style=%04X,          StyleIndex=%04X\n",brush->style,brush->style_index);
1652    logerror("Colour1=%04X,        Colour2=%04X\n",brush->colour1,brush->colour2);
1653    logerror("transparency=%04X,   boundry_spec=%04X\n",brush->transparency,brush->boundary_spec);
1654    logerror("boundry colour=%04X, save colour=%04X\n",brush->boundary_colour,brush->save_colour);
1655
1656
1657    OUTPUT_SEGOFS("SegData:OfsData",area_params->seg_data,area_params->ofs_data);
1658
1659    addr_ptr = memory_get_read_ptr(space, LINEAR_ADDR(area_params->seg_data,area_params->ofs_data));
1660    for(cocount=0; cocount < area_params->count; cocount++)
1661    {
1662        logerror("x=%d y=%d\n",addr_ptr[cocount*2],addr_ptr[(cocount*2)+1]);
1663    }
1664}
1665
1666static void decode_dssi_f_plot_character_string(const device_config *device,UINT16  ds, UINT16 si)
1667{
1668    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1669
1670    UINT8          *char_ptr;
1671    t_plot_string_params   *plot_string_params;
1672    int             charno;
1673
1674    plot_string_params=get_dssi_ptr(space,ds,si);
1675
1676    OUTPUT_SEGOFS("SegFont:OfsFont",plot_string_params->seg_font,plot_string_params->ofs_font);
1677    OUTPUT_SEGOFS("SegData:OfsData",plot_string_params->seg_data,plot_string_params->ofs_data);
1678
1679    logerror("x=%d, y=%d, length=%d\n",plot_string_params->x,plot_string_params->y,plot_string_params->length);
1680
1681    char_ptr=memory_get_read_ptr(space, LINEAR_ADDR(plot_string_params->seg_data,plot_string_params->ofs_data));
1682
1683    if (plot_string_params->length==0xFFFF)
1684        logerror("%s",char_ptr);
1685    else
1686        for(charno=0;charno<plot_string_params->length;charno++)
1687            logerror("%c",char_ptr[charno]);
1688
1689    logerror("\n");
1690}
1691
1692static void decode_dssi_f_set_new_clt(const device_config *device,UINT16  ds, UINT16 si)
1693{
1694    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1695    UINT16  *new_colours;
1696    int     colour;
1697    new_colours=get_dssi_ptr(space,ds,si);
1698
1699    OUTPUT_SEGOFS("SegColours:OfsColours",ds,si);
1700
1701    for(colour=0;colour<16;colour++)
1702        logerror("colour #%02X=%04X\n",colour,new_colours[colour]);
1703
1704}
1705
1706READ16_HANDLER( nimbus_io_r )
1707{
1708    int pc=cpu_get_pc(space->cpu);
1709
1710    logerror("Nimbus IOR at pc=%08X from %04X mask=%04X, data=%04X\n",pc,(offset*2)+0x30,mem_mask,IOPorts[offset]);
1711
1712    switch (offset*2)
1713    {
1714        default         : return IOPorts[offset]; break;
1715    }
1716    return 0;
1717}
1718
1719WRITE16_HANDLER( nimbus_io_w )
1720{
1721    int pc=cpu_get_pc(space->cpu);
1722
1723    logerror("Nimbus IOW at %08X write of %04X to %04X mask=%04X\n",pc,data,(offset*2)+0x30,mem_mask);
1724
1725    switch (offset*2)
1726    {
1727        default         : COMBINE_DATA(&IOPorts[offset]); break;
1728    }
1729}
1730
1731
1732/*
1733    Keyboard emulation
1734
1735*/
1736
1737static void keyboard_reset(void)
1738{
1739    memset(keyboard.keyrows,0xFF,NIMBUS_KEYROWS);
1740
1741    // Setup timer to scan keyboard.
1742    timer_adjust_periodic(keyboard.keyscan_timer, attotime_zero, 0, ATTOTIME_IN_HZ(50));
1743
1744}
1745
1746static void queue_scancode(UINT8 scancode)
1747{
1748   keyboard.queue[keyboard.head] = scancode;
1749   keyboard.head++;
1750   keyboard.head %= ARRAY_LENGTH(keyboard.queue);
1751}
1752
1753static int keyboard_queue_read(void)
1754{
1755   int data;
1756   if (keyboard.tail == keyboard.head)
1757      return -1;
1758
1759   data = keyboard.queue[keyboard.tail];
1760
1761   if (LOG_KEYBOARD)
1762      logerror("at_keyboard_read(): Keyboard Read 0x%02x\n",data);
1763
1764   keyboard.tail++;
1765   keyboard.tail %= ARRAY_LENGTH(keyboard.queue);
1766   return data;
1767}
1768
1769static TIMER_CALLBACK(keyscan_callback)
1770{
1771    UINT8   keyrow;
1772    UINT8   row;
1773    UINT8   bitno;
1774    UINT8   mask;
1775    static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3", "KEY4",
1776                                             "KEY5", "KEY6", "KEY7", "KEY8", "KEY9",
1777                                             "KEY10"};
1778
1779    for(row=0;row<NIMBUS_KEYROWS;row++)
1780    {
1781        keyrow=input_port_read(machine, keynames[row]);
1782
1783        for(mask=0x80, bitno=7;mask>0;mask=mask>>1, bitno-=1)
1784        {
1785            if(!(keyrow & mask) && (keyboard.keyrows[row] & mask))
1786            {
1787                logerror("keypress %02X\n",(row<<3)+bitno);
1788                queue_scancode((row<<3)+bitno);
1789            }
1790
1791            if((keyrow & mask) && !(keyboard.keyrows[row] & mask))
1792            {
1793                logerror("keyrelease %02X\n",0x80+(row<<3)+bitno);
1794                queue_scancode(0x80+(row<<3)+bitno);
1795            }
1796        }
1797
1798        keyboard.keyrows[row]=keyrow;
1799    }
1800}
1801
1802
1803/*
1804
1805Z80SIO, used for the keyboard interface
1806
1807*/
1808
1809READ8_DEVICE_HANDLER( sio_r )
1810{
1811    int pc=cpu_get_pc(cputag_get_cpu(device->machine,MAINCPU_TAG));
1812    UINT8 result = 0;
1813
1814   switch (offset*2)
1815   {
1816        case 0 :
1817            result=z80sio_d_r(device, 0);
1818            break;
1819        case 2 :
1820            result=z80sio_d_r(device, 1);
1821            break;
1822        case 4 :
1823            result=z80sio_c_r(device, 0);
1824            break;
1825        case 6 :
1826            result=z80sio_c_r(device, 1);
1827            break;
1828   }
1829
1830    logerror("Nimbus SIOR at pc=%08X from %04X data=%02X\n",pc,(offset*2)+0xF0,result);
1831
1832   return result;
1833}
1834
1835WRITE8_DEVICE_HANDLER( sio_w )
1836{
1837    int pc=cpu_get_pc(cputag_get_cpu(device->machine,MAINCPU_TAG));
1838
1839    logerror("Nimbus SIOW at %08X write of %02X to %04X\n",pc,data,(offset*2)+0xF0);
1840
1841   switch (offset*2)
1842   {
1843        case 0 :
1844            z80sio_d_w(device, 0, data);
1845            break;
1846        case 2 :
1847            z80sio_d_w(device, 1, data);
1848            break;
1849        case 4 :
1850            z80sio_c_w(device, 0, data);
1851            break;
1852        case 5 :
1853            z80sio_c_w(device, 1, data);
1854            break;
1855   }
1856}
1857
1858/* Z80 SIO/2 */
1859
1860void sio_interrupt(const device_config *device, int state)
1861{
1862    logerror("SIO Interrupt state=%02X\n",state);
1863
1864    // Don't re-trigger if already active !
1865    if(state!=sio_int_state)
1866    {
1867        sio_int_state=state;
1868
1869        if(state)
1870            external_int(device->machine,0,0x9C);
1871    }
1872}
1873
1874WRITE8_DEVICE_HANDLER( sio_dtr_w )
1875{
1876   if (offset == 1)
1877   {
1878   }
1879}
1880
1881WRITE8_DEVICE_HANDLER( sio_serial_transmit )
1882{
1883}
1884
1885int sio_serial_receive( const device_config *device, int channel )
1886{
1887   if(channel==0)
1888    {
1889        return keyboard_queue_read();
1890    }
1891    else
1892        return -1;
1893}
1894
1895/* Floppy disk */
1896
1897static void fdc_reset(void)
1898{
1899    nimbus_fdc.irq_enabled=0;
1900    nimbus_fdc.drq_enabled=0;
1901    nimbus_fdc.diskno=0;
1902    nimbus_fdc.motor_on=0;
1903}
1904
1905static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_intrq_w )
1906{
1907   logerror("nimbus_fdc_intrq_w(%d)\n", state);
1908
1909    if(state && nimbus_fdc.irq_enabled)
1910    {
1911        external_int(device->machine,0,0x80);
1912    }
1913}
1914
1915static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_drq_w )
1916{
1917   logerror("nimbus_fdc_drq_w(%d)\n", state);
1918
1919    if(state && nimbus_fdc.drq_enabled)
1920        drq_callback(device->machine,1);
1921}
1922
1923READ8_HANDLER( nimbus_fdc_r )
1924{
1925      int result = 0;
1926   const device_config *fdc = devtag_get_device(space->machine, FDC_TAG);
1927    int pc=cpu_get_pc(space->cpu);
1928    const device_config *drive = devtag_get_device(space->machine, nimbus_wd17xx_interface.floppy_drive_tags[nimbus_fdc.diskno]);
1929
1930    switch(offset*2)
1931   {
1932      case 0x08 :
1933         result = wd17xx_status_r(fdc, 0);
1934         logerror("Disk status=%2.2X\n",result);
1935         break;
1936      case 0x0A :
1937         result = wd17xx_track_r(fdc, 0);
1938         break;
1939      case 0x0C :
1940         result = wd17xx_sector_r(fdc, 0);
1941         break;
1942      case 0x0E :
1943         result = wd17xx_data_r(fdc, 0);
1944         break;
1945        case 0x10 :
1946            result = (nimbus_fdc.motor_on                                    ? 0x04 : 0x00) |
1947                     (floppy_drive_get_flag_state(drive, FLOPPY_DRIVE_INDEX) ? 0x00 : 0x02) |
1948                     (floppy_drive_get_flag_state(drive, FLOPPY_DRIVE_READY) ? 0x01 : 0x00);
1949            break;
1950      default:
1951         break;
1952   }
1953
1954    logerror("Nimbus FDCR at pc=%08X from %04X data=%02X\n",pc,(offset*2)+0x400,result);
1955
1956   return result;
1957}
1958
1959WRITE8_HANDLER( nimbus_fdc_w )
1960{
1961   const device_config *fdc = devtag_get_device(space->machine, FDC_TAG);
1962    int pc=cpu_get_pc(space->cpu);
1963
1964    logerror("Nimbus FDCW at %08X write of %02X to %04X\n",pc,data,(offset*2)+0x400);
1965
1966    switch(offset*2)
1967   {
1968        case 0x00 :
1969            switch (data & 0x0F)
1970            {
1971                case 0x01 : nimbus_fdc.diskno=0; break;
1972                case 0x02 : nimbus_fdc.diskno=1; break;
1973                case 0x04 : nimbus_fdc.diskno=2; break;
1974                case 0x08 : nimbus_fdc.diskno=3; break;
1975                //default : nimbus_fdc.diskno=NO_DRIVE_SELECTED; break; // no drive selected
1976            }
1977            if (nimbus_fdc.diskno!=NO_DRIVE_SELECTED)
1978            {
1979                wd17xx_set_drive(fdc, nimbus_fdc.diskno);
1980                wd17xx_set_side(fdc, (data & 0x10) ? 1 : 0);
1981            }
1982            nimbus_fdc.motor_on=(data & 0x20);
1983            nimbus_fdc.drq_enabled=(data & 0x80);
1984            nimbus_fdc.irq_enabled=1;
1985
1986            // Nimbus FDC is hard wired for double density
1987            wd17xx_set_density(fdc, DEN_MFM_LO);
1988         break;
1989      case 0x08 :
1990         wd17xx_command_w(fdc, 0, data);
1991         break;
1992      case 0x0A :
1993         wd17xx_track_w(fdc, 0, data);
1994         break;
1995      case 0x0C :
1996         wd17xx_sector_w(fdc, 0, data);
1997         break;
1998      case 0x0E :
1999         wd17xx_data_w(fdc, 0, data);
2000         break;
2001   }
2002}
2003
1/* 
2    machine/rmnimbus.c
3     
4    Machine driver for the Research Machines Nimbus.
5     
6    Phill Harvey-Smith
7    2009-11-29.
8     
9    80186 internal DMA/Timer/PIC code borrowed from Compis driver.
10    Perhaps this needs merging into the 80186 core.....
11*/
12 
13 
14#include "driver.h"
15#include "memory.h"
16#include "cpu/i86/i86.h"
17#include "debug/debugcpu.h"
18#include "debug/debugcon.h"
19#include "devices/flopdrv.h"
20#include "machine/pic8259.h"
21#include "machine/pit8253.h"
22#include "machine/msm8251.h"
23#include "machine/z80sio.h"
24 
25#include "includes/rmnimbus.h"
26 
27/*-------------------------------------------------------------------------*/
28/* Defines, constants, and global variables                                */
29/*-------------------------------------------------------------------------*/
30 
31/* CPU 80186 */
32#define LATCH_INTS          1
33#define LOG_PORTS           0
34#define LOG_INTERRUPTS      0
35#define LOG_INTERRUPTS_EXT  0
36#define LOG_TIMER           0
37#define LOG_OPTIMIZATION    0
38#define LOG_DMA             0
39#define CPU_RESUME_TRIGGER   7123
40#define LOG_KEYBOARD        0
41#define LOG_SIO             0
42#define LOG_DISK            0
43 
44/* 80186 internal stuff */
45struct mem_state
46{
47   UINT16       lower;
48   UINT16       upper;
49   UINT16       middle;
50   UINT16       middle_size;
51   UINT16       peripheral;
52};
53 
54struct timer_state
55{
56   UINT16       control;
57   UINT16       maxA;
58   UINT16       maxB;
59   UINT16       count;
60   emu_timer   *int_timer;
61   emu_timer   *time_timer;
62   UINT8       time_timer_active;
63   attotime   last_time;
64};
65 
66struct dma_state
67{
68   UINT32       source;
69   UINT32       dest;
70   UINT16       count;
71   UINT16       control;
72   UINT8       finished;
73   emu_timer   *finish_timer;
74};
75 
76struct intr_state
77{
78   UINT8   pending;
79   UINT16   ack_mask;
80   UINT16   priority_mask;
81   UINT16   in_service;
82   UINT16   request;
83   UINT16   status;
84   UINT16   poll_status;
85   UINT16   timer;
86   UINT16   dma[2];
87   UINT16   ext[4];
88    UINT16  ext_vector[2]; // external vectors, when in cascade mode
89};
90 
91static struct i186_state
92{
93   struct timer_state   timer[3];
94   struct dma_state   dma[2];
95   struct intr_state   intr;
96   struct mem_state   mem;
97} i186;
98 
99 
100/* Z80 SIO */
101 
102const z80sio_interface sio_intf =
103{
104   sio_interrupt,         /* interrupt handler */
105   0, //sio_dtr_w,            /* DTR changed handler */
106   0,                  /* RTS changed handler */
107   0,                  /* BREAK changed handler */
108   sio_serial_transmit,   /* transmit handler */
109   sio_serial_receive      /* receive handler */
110};
111 
112struct keybord_state
113{
114    UINT8       keyrows[NIMBUS_KEYROWS];
115    emu_timer   *keyscan_timer;
116    UINT8       queue[KEYBOARD_QUEUE_SIZE];
117   UINT8       head;
118   UINT8       tail;
119} keyboard;
120 
121 
122 
123/* Floppy drives WD2793 */
124 
125static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_intrq_w );
126static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_drq_w );
127 
128const wd17xx_interface nimbus_wd17xx_interface =
129{
130   DEVCB_LINE(nimbus_fdc_intrq_w),
131   DEVCB_LINE(nimbus_fdc_drq_w),
132   {FLOPPY_0, FLOPPY_1, FLOPPY_2, FLOPPY_3}
133};
134 
135static struct _nimbus_fdc
136{
137    UINT8 drq_enabled;
138    UINT8 irq_enabled;
139    UINT8 diskno;
140    UINT8 motor_on;
141} nimbus_fdc;
142 
143 
144static void drq_callback(running_machine *machine, int which);
145 
146static void execute_debug_irq(running_machine *machine, int ref, int params, const char *param[]);
147static void execute_debug_intmasks(running_machine *machine, int ref, int params, const char *param[]);
148 
149static int instruction_hook(const device_config *device, offs_t curpc);
150static void decode_subbios(const device_config *device,offs_t pc);
151static void decode_dssi_f_fill_area(const device_config *device,UINT16  ds, UINT16 si);
152static void decode_dssi_f_plot_character_string(const device_config *device,UINT16  ds, UINT16 si);
153static void decode_dssi_f_set_new_clt(const device_config *device,UINT16  ds, UINT16 si);
154static void decode_dssi_f_plonk_char(const device_config *device,UINT16  ds, UINT16 si);
155 
156static void fdc_reset(void);
157 
158static void keyboard_reset(void);
159static TIMER_CALLBACK(keyscan_callback);
160 
161#define num_ioports 0x80
162static UINT16   IOPorts[num_ioports];
163 
164UINT8 nextkey;
165UINT8 sio_int_state;
166 
167/*************************************
168 *
169 *  80186 interrupt controller
170 *
171 *************************************/
172static IRQ_CALLBACK(int_callback)
173{
174    UINT8   vector;
175    UINT16  old;
176    UINT16  oldreq;
177 
178   if (LOG_INTERRUPTS)
179      logerror("(%f) **** Acknowledged interrupt vector %02X\n", attotime_to_double(timer_get_time(device->machine)), i186.intr.poll_status & 0x1f);
180 
181   /* clear the interrupt */
182   cpu_set_input_line(device, 0, CLEAR_LINE);
183   i186.intr.pending = 0;
184 
185    oldreq=i186.intr.request;
186 
187   /* clear the request and set the in-service bit */
188#if LATCH_INTS
189   i186.intr.request &= ~i186.intr.ack_mask;
190#else
191   i186.intr.request &= ~(i186.intr.ack_mask & 0x0f);
192#endif
193 
194   if((LOG_INTERRUPTS) && (i186.intr.request!=oldreq))
195        logerror("i186.intr.request changed from %02X to %02X\n",oldreq,i186.intr.request);
196 
197    old=i186.intr.in_service;
198 
199   i186.intr.in_service |= i186.intr.ack_mask;
200   
201    if((LOG_INTERRUPTS) && (i186.intr.in_service!=old))
202        logerror("i186.intr.in_service changed from %02X to %02X\n",old,i186.intr.in_service);
203     
204    if (i186.intr.ack_mask == 0x0001)
205   {
206      switch (i186.intr.poll_status & 0x1f)
207      {
208         case 0x08:   i186.intr.status &= ~0x01;   break;
209         case 0x12:   i186.intr.status &= ~0x02;   break;
210         case 0x13:   i186.intr.status &= ~0x04;   break;
211      }
212   }
213   i186.intr.ack_mask = 0;
214 
215   /* a request no longer pending */
216   i186.intr.poll_status &= ~0x8000;
217 
218   /* return the vector */
219    switch(i186.intr.poll_status & 0x1F)
220    {
221        case 0x0C   : vector=(i186.intr.ext[0] & EXTINT_CTRL_CASCADE) ? i186.intr.ext_vector[0] : (i186.intr.poll_status & 0x1f); break;
222        case 0x0D   : vector=(i186.intr.ext[1] & EXTINT_CTRL_CASCADE) ? i186.intr.ext_vector[1] : (i186.intr.poll_status & 0x1f); break;
223        default :
224            vector=i186.intr.poll_status & 0x1f; break;
225    }
226 
227    if (LOG_INTERRUPTS_EXT)
228   {
229        logerror("i186.intr.ext[0]=%04X i186.intr.ext[1]=%04X\n",i186.intr.ext[0],i186.intr.ext[1]);
230        logerror("Ext vectors : %02X %02X\n",i186.intr.ext_vector[0],i186.intr.ext_vector[1]);
231        logerror("Int %02X Calling vector %02X\n",i186.intr.poll_status,vector);
232    }
233     
234    return vector;
235}
236 
237 
238static void update_interrupt_state(running_machine *machine)
239{   
240    int new_vector = 0;
241    int Priority;
242    int IntNo;
243 
244   if (LOG_INTERRUPTS) 
245        logerror("update_interrupt_status: req=%04X stat=%04X serv=%04X priority_mask=%4X\n", i186.intr.request, i186.intr.status, i186.intr.in_service, i186.intr.priority_mask);
246 
247   /* loop over priorities */
248   for (Priority = 0; Priority <= i186.intr.priority_mask; Priority++)
249   {
250      /* note: by checking 4 bits, we also verify that the mask is off */
251      if ((i186.intr.timer & 15) == Priority)
252      {
253         /* if we're already servicing something at this level, don't generate anything new */
254         if (i186.intr.in_service & 0x01)
255            return;
256 
257         /* if there's something pending, generate an interrupt */
258         if (i186.intr.status & 0x07)
259         {
260            if (i186.intr.status & 1)
261               new_vector = 0x08;
262            else if (i186.intr.status & 2)
263               new_vector = 0x12;
264            else if (i186.intr.status & 4)
265               new_vector = 0x13;
266            else
267               popmessage("Invalid timer interrupt!");
268 
269            /* set the clear mask and generate the int */
270            i186.intr.ack_mask = 0x0001;
271            goto generate_int;
272         }
273      }
274 
275      /* check DMA interrupts */
276      for (IntNo = 0; IntNo < 2; IntNo++)
277         if ((i186.intr.dma[IntNo] & 0x0F) == Priority)
278         {
279            /* if we're already servicing something at this level, don't generate anything new */
280            if (i186.intr.in_service & (0x04 << IntNo))
281               return;
282 
283            /* if there's something pending, generate an interrupt */
284            if (i186.intr.request & (0x04 << IntNo))
285            {
286               new_vector = 0x0a + IntNo;
287 
288               /* set the clear mask and generate the int */
289               i186.intr.ack_mask = 0x0004 << IntNo;
290               goto generate_int;
291            }
292         }
293 
294        //logerror("Checking external ints\n");
295      /* check external interrupts */
296      for (IntNo = 0; IntNo < 4; IntNo++)
297         if ((i186.intr.ext[IntNo] & 0x0F) == Priority)
298         {
299                if (LOG_INTERRUPTS)
300                    logerror("Int%d priority=%d\n",IntNo,Priority);
301                 
302                /* if we're already servicing something at this level, don't generate anything new */
303            if (i186.intr.in_service & (0x10 << IntNo))
304               return;
305 
306            /* if there's something pending, generate an interrupt */
307            if (i186.intr.request & (0x10 << IntNo))
308            {
309               /* otherwise, generate an interrupt for this request */
310               new_vector = 0x0c + IntNo;
311 
312               /* set the clear mask and generate the int */
313               i186.intr.ack_mask = 0x0010 << IntNo;
314                    goto generate_int;
315            }
316         }
317   }
318   return;
319 
320generate_int:
321   /* generate the appropriate interrupt */
322   i186.intr.poll_status = 0x8000 | new_vector;
323   if (!i186.intr.pending)
324      cputag_set_input_line(machine, MAINCPU_TAG, 0, ASSERT_LINE);
325   i186.intr.pending = 1;
326   cpuexec_trigger(machine, CPU_RESUME_TRIGGER);
327   if (LOG_OPTIMIZATION) logerror("  - trigger due to interrupt pending\n");
328   if (LOG_INTERRUPTS) logerror("(%f) **** Requesting interrupt vector %02X\n", attotime_to_double(timer_get_time(machine)), new_vector);
329}
330 
331 
332static void handle_eoi(running_machine *machine,int data)
333{
334    int Priority;
335    int IntNo;
336 
337   /* specific case */
338   if (!(data & 0x8000))
339   {
340      /* turn off the appropriate in-service bit */
341      switch (data & 0x1f)
342      {
343         case 0x08:   i186.intr.in_service &= ~0x01;   break;
344         case 0x12:   i186.intr.in_service &= ~0x01;   break;
345         case 0x13:   i186.intr.in_service &= ~0x01;   break;
346         case 0x0a:   i186.intr.in_service &= ~0x04;   break;
347         case 0x0b:   i186.intr.in_service &= ~0x08;   break;
348         case 0x0c:   i186.intr.in_service &= ~0x10;   break;
349         case 0x0d:   i186.intr.in_service &= ~0x20;   break;
350         case 0x0e:   i186.intr.in_service &= ~0x40;   break;
351         case 0x0f:   i186.intr.in_service &= ~0x80;   break;
352         default:   logerror("%05X:ERROR - 80186 EOI with unknown vector %02X\n", cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), data & 0x1f);
353      }
354      if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for vector %02X\n", attotime_to_double(timer_get_time(machine)), data & 0x1f);
355   }
356 
357   /* non-specific case */
358   else
359   {
360      /* loop over priorities */
361      for (Priority = 0; Priority <= 7; Priority++)
362      {
363         /* check for in-service timers */
364         if ((i186.intr.timer & 0x07) == Priority && (i186.intr.in_service & 0x01))
365         {
366            i186.intr.in_service &= ~0x01;
367            if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for timer\n", attotime_to_double(timer_get_time(machine)));
368            return;
369         }
370 
371         /* check for in-service DMA interrupts */
372         for (IntNo = 0; IntNo < 2; IntNo++)
373            if ((i186.intr.dma[IntNo] & 0x07) == Priority && (i186.intr.in_service & (0x04 << IntNo)))
374            {
375               i186.intr.in_service &= ~(0x04 << IntNo);
376               if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for DMA%d\n", attotime_to_double(timer_get_time(machine)), IntNo);
377               return;
378            }
379 
380         /* check external interrupts */
381         for (IntNo = 0; IntNo < 4; IntNo++)
382            if ((i186.intr.ext[IntNo] & 0x07) == Priority && (i186.intr.in_service & (0x10 << IntNo)))
383            {
384               i186.intr.in_service &= ~(0x10 << IntNo);
385               if (LOG_INTERRUPTS) logerror("(%f) **** Got EOI for INT%d\n", attotime_to_double(timer_get_time(machine)), IntNo);
386               return;
387            }
388      }
389   }
390}
391 
392/* Trigger an external interupt, optionally supplying the vector to take */
393static void external_int(running_machine *machine, UINT16 intno, UINT8 vector)
394{
395   if (LOG_INTERRUPTS_EXT) logerror("generating external int %02X, vector %02X\n",intno,vector);
396 
397   // Only 4 external ints
398    if(intno>3)
399    {
400        logerror("external_int() invalid external interupt no : 0x%02X (can only be 0..3)\n",intno);
401        return;
402    }
403     
404    // Only set external vector if cascade mode enabled, only valid for
405    // int 0 & int 1
406    if (intno<2)
407    {
408        if(i186.intr.ext[intno] & EXTINT_CTRL_CASCADE)
409            i186.intr.ext_vector[intno]=vector;
410    }
411     
412    // Turn on the requested request bit and handle interrupt
413    i186.intr.request |= (0x010 << intno);
414    update_interrupt_state(machine);
415}
416 
417 
418/*************************************
419 *
420 *  80186 internal timers
421 *
422 *************************************/
423 
424static TIMER_CALLBACK(internal_timer_int)
425{
426   int which = param;
427   struct timer_state *t = &i186.timer[which];
428 
429   if (LOG_TIMER) logerror("Hit interrupt callback for timer %d\n", which);
430 
431   /* set the max count bit */
432   t->control |= 0x0020;
433 
434   /* request an interrupt */
435   if (t->control & 0x2000)
436   {
437      i186.intr.status |= 0x01 << which;
438      update_interrupt_state(machine);
439      if (LOG_TIMER) logerror("  Generating timer interrupt\n");
440   }
441 
442   /* if we're continuous, reset */
443   if (t->control & 0x0001)
444   {
445      int count = t->maxA ? t->maxA : 0x10000;
446      timer_adjust_oneshot(t->int_timer, attotime_mul(ATTOTIME_IN_HZ(2000000), count), which);
447      if (LOG_TIMER) logerror("  Repriming interrupt\n");
448   }
449   else
450      timer_adjust_oneshot(t->int_timer, attotime_never, which);
451}
452 
453 
454static void internal_timer_sync(int which)
455{
456   struct timer_state *t = &i186.timer[which];
457 
458   /* if we have a timing timer running, adjust the count */
459   if (t->time_timer_active)
460   {
461      attotime current_time = timer_timeelapsed(t->time_timer);
462      int net_clocks = attotime_mul(attotime_sub(current_time, t->last_time),  2000000).seconds;
463      t->last_time = current_time;
464 
465      /* set the max count bit if we passed the max */
466      if ((int)t->count + net_clocks >= t->maxA)
467         t->control |= 0x0020;
468 
469      /* set the new count */
470      if (t->maxA != 0)
471         t->count = (t->count + net_clocks) % t->maxA;
472      else
473         t->count = t->count + net_clocks;
474   }
475}
476 
477 
478static void internal_timer_update(running_machine *machine,
479                          int which,
480                                  int new_count,
481                                  int new_maxA,
482                                  int new_maxB,
483                                  int new_control)
484{
485   struct timer_state *t = &i186.timer[which];
486   int update_int_timer = 0;
487 
488    if (LOG_TIMER) 
489        logerror("internal_timer_update: %d, new_count=%d, new_maxA=%d, new_maxB=%d,new_control=%d\n",which,new_count,new_maxA,new_maxB,new_control);
490 
491   /* if we have a new count and we're on, update things */
492   if (new_count != -1)
493   {
494      if (t->control & 0x8000)
495      {
496         internal_timer_sync(which);
497         update_int_timer = 1;
498      }
499      t->count = new_count;
500   }
501 
502   /* if we have a new max and we're on, update things */
503   if (new_maxA != -1 && new_maxA != t->maxA)
504   {
505      if (t->control & 0x8000)
506      {
507         internal_timer_sync(which);
508         update_int_timer = 1;
509      }
510      t->maxA = new_maxA;
511      if (new_maxA == 0)
512      {
513               new_maxA = 0x10000;
514      }
515   }
516 
517   /* if we have a new max and we're on, update things */
518   if (new_maxB != -1 && new_maxB != t->maxB)
519   {
520      if (t->control & 0x8000)
521      {
522         internal_timer_sync(which);
523         update_int_timer = 1;
524      }
525 
526      t->maxB = new_maxB;
527 
528      if (new_maxB == 0)
529      {
530               new_maxB = 0x10000;
531            }
532      }
533     
534 
535   /* handle control changes */
536   if (new_control != -1)
537   {
538      int diff;
539 
540      /* merge back in the bits we don't modify */
541      new_control = (new_control & ~0x1fc0) | (t->control & 0x1fc0);
542 
543      /* handle the /INH bit */
544      if (!(new_control & 0x4000))
545         new_control = (new_control & ~0x8000) | (t->control & 0x8000);
546      new_control &= ~0x4000;
547 
548      /* check for control bits we don't handle */
549      diff = new_control ^ t->control;
550      if (diff & 0x001c)
551        logerror("%05X:ERROR! -unsupported timer mode %04X\n",
552            cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), new_control);
553 
554      /* if we have real changes, update things */
555      if (diff != 0)
556      {
557 
558         /* if we're going off, make sure our timers are gone */
559         if ((diff & 0x8000) && !(new_control & 0x8000))
560         {
561            /* compute the final count */
562            internal_timer_sync(which);
563 
564            /* nuke the timer and force the interrupt timer to be recomputed */
565            timer_adjust_oneshot(t->time_timer, attotime_never, which);
566            t->time_timer_active = 0;
567            update_int_timer = 1;
568         }
569 
570         /* if we're going on, start the timers running */
571         else if ((diff & 0x8000) && (new_control & 0x8000))
572         {
573            /* start the timing */
574            timer_adjust_oneshot(t->time_timer, attotime_never, which);
575            t->time_timer_active = 1;
576            update_int_timer = 1;
577         }
578 
579         /* if something about the interrupt timer changed, force an update */
580         if (!(diff & 0x8000) && (diff & 0x2000))
581         {
582            internal_timer_sync(which);
583            update_int_timer = 1;
584         }
585      }
586 
587      /* set the new control register */
588      t->control = new_control;
589   }
590 
591   /* update the interrupt timer */
592      if (update_int_timer)
593      {
594            if ((t->control & 0x8000) && (t->control & 0x2000))
595            {
596              int diff = t->maxA - t->count;
597               if (diff <= 0)
598                  diff += 0x10000;
599               timer_adjust_oneshot(t->int_timer, attotime_mul(ATTOTIME_IN_HZ(2000000), diff), which);
600               if (LOG_TIMER) logerror("Set interrupt timer for %d\n", which);
601            }
602            else
603            {
604              timer_adjust_oneshot(t->int_timer, attotime_never, which);
605      }
606   }
607}
608 
609 
610 
611/*************************************
612 *
613 *  80186 internal DMA
614 *
615 *************************************/
616 
617static TIMER_CALLBACK(dma_timer_callback)
618{
619   int which = param;
620   struct dma_state *d = &i186.dma[which];
621 
622   /* force an update and see if we're really done */
623   //stream_update(dma_stream, 0);
624 
625   /* complete the status update */
626   d->control &= ~0x0002;
627   d->source += d->count;
628   d->count = 0;
629 
630   /* check for interrupt generation */
631   if (d->control & 0x0100)
632   {
633      if (LOG_DMA) logerror("DMA%d timer callback - requesting interrupt: count = %04X, source = %04X\n", which, d->count, d->source);
634      i186.intr.request |= 0x04 << which;
635      update_interrupt_state(machine);
636   }
637}
638 
639 
640static void update_dma_control(running_machine *machine, int which, int new_control)
641{
642   struct dma_state *d = &i186.dma[which];
643   int diff;
644 
645   /* handle the CHG bit */
646   if (!(new_control & CHG_NOCHG))
647     new_control = (new_control & ~ST_STOP) | (d->control & ST_STOP);
648   new_control &= ~CHG_NOCHG;
649 
650   /* check for control bits we don't handle */
651   diff = new_control ^ d->control;
652   if ((LOG_DMA) && (diff & 0x6811))
653     logerror("%05X:ERROR! - unsupported DMA mode %04X\n",
654         cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), new_control);
655 
656   /* if we're going live, set a timer */
657   if ((diff & 0x0002) && (new_control & 0x0002))
658   {
659      /* make sure the parameters meet our expectations */
660      if ((new_control & 0xfe00) != 0x1600)
661      {
662         if (LOG_DMA) logerror("Unexpected DMA control %02X\n", new_control);
663      }
664      else if (/*!is_redline &&*/ ((d->dest & 1) || (d->dest & 0x3f) > 0x0b))
665      {
666         if (LOG_DMA) logerror("Unexpected DMA destination %02X\n", d->dest);
667      }
668      else if (/*is_redline && */ (d->dest & 0xf000) != 0x4000 && (d->dest & 0xf000) != 0x5000)
669      {
670         if (LOG_DMA) logerror("Unexpected DMA destination %02X\n", d->dest);
671      }
672 
673      /* otherwise, set a timer */
674      else
675      {
676//          int count = d->count;
677 
678         /* adjust for redline racer */
679            // int dacnum = (d->dest & 0x3f) / 2;
680 
681         if (LOG_DMA) logerror("Initiated DMA %d - count = %04X, source = %04X, dest = %04X\n", which, d->count, d->source, d->dest);
682 
683         d->finished = 0;
684/*          timer_adjust_oneshot(d->finish_timer,
685         ATTOTIME_IN_HZ(dac[dacnum].frequency) * (double)count, which);*/
686      }
687   }
688 
689   /* set the new control register */
690   d->control = new_control;
691}
692 
693static void drq_callback(running_machine *machine, int which)
694{
695    struct dma_state *dma = &i186.dma[which];
696   const address_space *memory_space   = cpu_get_address_space(cputag_get_cpu(machine, MAINCPU_TAG), ADDRESS_SPACE_PROGRAM);
697    const address_space *io_space       = cpu_get_address_space(cputag_get_cpu(machine, MAINCPU_TAG), ADDRESS_SPACE_IO);
698     
699    const address_space *src_space;
700    const address_space *dest_space;
701     
702    UINT16  dma_word;
703    UINT8   dma_byte;
704    UINT8   incdec_size;
705 
706    if (LOG_DMA)
707        logerror("Control=%04X, src=%05X, dest=%05X, count=%04X\n",dma->control,dma->source,dma->dest,dma->count);
708 
709    if(!(dma->control & ST_STOP))
710    {
711        logerror("%05X:ERROR! - drq%d with dma channel stopped\n",
712         cpu_get_pc(cputag_get_cpu(machine, MAINCPU_TAG)), which);
713           
714        return;
715    }
716 
717    if(dma->control & DEST_MIO)
718        dest_space=memory_space;
719    else
720        dest_space=io_space;
721     
722    if(dma->control & SRC_MIO)
723        src_space=memory_space;
724    else
725        src_space=io_space;
726     
727    // Do the transfer
728    if(dma->control & BYTE_WORD)
729    {
730        dma_word=memory_read_word(src_space,dma->source);
731        memory_write_word(dest_space,dma->dest,dma_word);
732        incdec_size=2;
733    }
734    else
735    {
736        dma_byte=memory_read_byte(src_space,dma->source);
737        memory_write_byte(dest_space,dma->dest,dma_byte);
738        incdec_size=1;
739    }
740     
741    // Increment or Decrement destination ans source pointers as needed
742    switch (dma->control & DEST_INCDEC_MASK)
743    {
744        case DEST_DECREMENT     : dma->dest -= incdec_size;
745        case DEST_INCREMENT     : dma->dest += incdec_size;
746    }
747     
748    switch (dma->control & SRC_INCDEC_MASK)
749    {
750        case SRC_DECREMENT     : dma->source -= incdec_size;
751        case SRC_INCREMENT     : dma->source += incdec_size;
752    }
753     
754    // decrement count
755    dma->count -= 1;
756     
757    // Terminate if count is zero, and terminate flag set
758    if((dma->control & TERMINATE_ON_ZERO) && (dma->count==0))
759    {
760        dma->control &= ~ST_STOP;
761        if (LOG_DMA) logerror("DMA terminated\n");
762    }
763     
764    // Interrupt if count is zero, and interrupt flag set
765    if((dma->control & INTERRUPT_ON_ZERO) && (dma->count==0))
766    {
767      if (LOG_DMA) logerror("DMA%d - requesting interrupt: count = %04X, source = %04X\n", which, dma->count, dma->source);
768      i186.intr.request |= 0x04 << which;
769      update_interrupt_state(machine);       
770    }
771}
772 
773/*-------------------------------------------------------------------------*/
774/* Name: rmnimbus                                                            */
775/* Desc: CPU - Initialize the 80186 CPU                                    */
776/*-------------------------------------------------------------------------*/
777static void nimbus_cpu_init(running_machine *machine)
778{
779 
780    logerror("Machine reset\n");
781     
782   /* create timers here so they stick around */
783   i186.timer[0].int_timer = timer_alloc(machine, internal_timer_int, NULL);
784   i186.timer[1].int_timer = timer_alloc(machine, internal_timer_int, NULL);
785   i186.timer[2].int_timer = timer_alloc(machine, internal_timer_int, NULL);
786   i186.timer[0].time_timer = timer_alloc(machine, NULL, NULL);
787   i186.timer[1].time_timer = timer_alloc(machine, NULL, NULL);
788   i186.timer[2].time_timer = timer_alloc(machine, NULL, NULL);
789   i186.dma[0].finish_timer = timer_alloc(machine, dma_timer_callback, NULL);
790   i186.dma[1].finish_timer = timer_alloc(machine, dma_timer_callback, NULL);
791}
792 
793static void nimbus_cpu_reset(running_machine *machine)
794{
795      /* reset the interrupt state */
796   i186.intr.priority_mask       = 0x0007;
797   i186.intr.timer          = 0x000f;
798   i186.intr.dma[0]         = 0x000f;
799   i186.intr.dma[1]         = 0x000f;
800   i186.intr.ext[0]         = 0x000f;
801   i186.intr.ext[1]         = 0x000f;
802   i186.intr.ext[2]         = 0x000f;
803   i186.intr.ext[3]         = 0x000f;
804    i186.intr.in_service        = 0x0000;
805     
806    /* External vectors by default to internal int 0/1 vectors */
807    i186.intr.ext_vector[0]      = 0x000C;
808   i186.intr.ext_vector[1]      = 0x000D;
809   
810    i186.intr.pending           = 0x0000;
811   i186.intr.ack_mask          = 0x0000;
812   i186.intr.request           = 0x0000;
813   i186.intr.status            = 0x0000;
814   i186.intr.poll_status       = 0x0000;
815   
816    logerror("CPU reset done\n");
817}
818 
819READ16_HANDLER( i186_internal_port_r )
820{
821   int temp, which;
822 
823   switch (offset)
824   {
825      case 0x11:
826         logerror("%05X:ERROR - read from 80186 EOI\n", cpu_get_pc(space->cpu));
827         break;
828 
829      case 0x12:
830         if (LOG_PORTS) logerror("%05X:read 80186 interrupt poll\n", cpu_get_pc(space->cpu));
831         if (i186.intr.poll_status & 0x8000)
832            int_callback(cputag_get_cpu(space->machine, MAINCPU_TAG), 0);
833         return i186.intr.poll_status;
834 
835      case 0x13:
836         if (LOG_PORTS) logerror("%05X:read 80186 interrupt poll status\n", cpu_get_pc(space->cpu));
837         return i186.intr.poll_status;
838 
839      case 0x14:
840         if (LOG_PORTS) logerror("%05X:read 80186 interrupt mask\n", cpu_get_pc(space->cpu));
841         temp  = (i186.intr.timer  >> 3) & 0x01;
842         temp |= (i186.intr.dma[0] >> 1) & 0x04;
843         temp |= (i186.intr.dma[1] >> 0) & 0x08;
844         temp |= (i186.intr.ext[0] << 1) & 0x10;
845         temp |= (i186.intr.ext[1] << 2) & 0x20;
846         temp |= (i186.intr.ext[2] << 3) & 0x40;
847         temp |= (i186.intr.ext[3] << 4) & 0x80;
848         return temp;
849 
850      case 0x15:
851         if (LOG_PORTS) logerror("%05X:read 80186 interrupt priority mask\n", cpu_get_pc(space->cpu));
852         return i186.intr.priority_mask;
853 
854      case 0x16:
855         if (LOG_PORTS) logerror("%05X:read 80186 interrupt in-service\n", cpu_get_pc(space->cpu));
856         return i186.intr.in_service;
857 
858      case 0x17:
859         if (LOG_PORTS) logerror("%05X:read 80186 interrupt request\n", cpu_get_pc(space->cpu));
860         temp = i186.intr.request & ~0x0001;
861         if (i186.intr.status & 0x0007)
862            temp |= 1;
863         return temp;
864 
865      case 0x18:
866         if (LOG_PORTS) logerror("%05X:read 80186 interrupt status\n", cpu_get_pc(space->cpu));
867         return i186.intr.status;
868 
869      case 0x19:
870         if (LOG_PORTS) logerror("%05X:read 80186 timer interrupt control\n", cpu_get_pc(space->cpu));
871         return i186.intr.timer;
872 
873      case 0x1a:
874         if (LOG_PORTS) logerror("%05X:read 80186 DMA 0 interrupt control\n", cpu_get_pc(space->cpu));
875         return i186.intr.dma[0];
876 
877      case 0x1b:
878         if (LOG_PORTS) logerror("%05X:read 80186 DMA 1 interrupt control\n", cpu_get_pc(space->cpu));
879         return i186.intr.dma[1];
880 
881      case 0x1c:
882         if (LOG_PORTS) logerror("%05X:read 80186 INT 0 interrupt control\n", cpu_get_pc(space->cpu));
883         return i186.intr.ext[0];
884 
885      case 0x1d:
886         if (LOG_PORTS) logerror("%05X:read 80186 INT 1 interrupt control\n", cpu_get_pc(space->cpu));
887         return i186.intr.ext[1];
888 
889      case 0x1e:
890         if (LOG_PORTS) logerror("%05X:read 80186 INT 2 interrupt control\n", cpu_get_pc(space->cpu));
891         return i186.intr.ext[2];
892 
893      case 0x1f:
894         if (LOG_PORTS) logerror("%05X:read 80186 INT 3 interrupt control\n", cpu_get_pc(space->cpu));
895         return i186.intr.ext[3];
896 
897      case 0x28:
898      case 0x2c:
899      case 0x30:
900         if (LOG_PORTS) logerror("%05X:read 80186 Timer %d count\n", cpu_get_pc(space->cpu), (offset - 0x28) / 4);
901         which = (offset - 0x28) / 4;
902         if (!(offset & 1))
903            internal_timer_sync(which);
904         return i186.timer[which].count;
905 
906      case 0x29:
907      case 0x2d:
908      case 0x31:
909         if (LOG_PORTS) logerror("%05X:read 80186 Timer %d max A\n", cpu_get_pc(space->cpu), (offset - 0x29) / 4);
910         which = (offset - 0x29) / 4;
911         return i186.timer[which].maxA;
912 
913      case 0x2a:
914      case 0x2e:
915         logerror("%05X:read 80186 Timer %d max B\n", cpu_get_pc(space->cpu), (offset - 0x2a) / 4);
916         which = (offset - 0x2a) / 4;
917         return i186.timer[which].maxB;
918 
919      case 0x2b:
920      case 0x2f:
921      case 0x33:
922         if (LOG_PORTS) logerror("%05X:read 80186 Timer %d control\n", cpu_get_pc(space->cpu), (offset - 0x2b) / 4);
923         which = (offset - 0x2b) / 4;
924         return i186.timer[which].control;
925 
926      case 0x50:
927         if (LOG_PORTS) logerror("%05X:read 80186 upper chip select\n", cpu_get_pc(space->cpu));
928         return i186.mem.upper;
929 
930      case 0x51:
931         if (LOG_PORTS) logerror("%05X:read 80186 lower chip select\n", cpu_get_pc(space->cpu));
932         return i186.mem.lower;
933 
934      case 0x52:
935         if (LOG_PORTS) logerror("%05X:read 80186 peripheral chip select\n", cpu_get_pc(space->cpu));
936         return i186.mem.peripheral;
937 
938      case 0x53:
939         if (LOG_PORTS) logerror("%05X:read 80186 middle chip select\n", cpu_get_pc(space->cpu));
940         return i186.mem.middle;
941 
942      case 0x54:
943         if (LOG_PORTS) logerror("%05X:read 80186 middle P chip select\n", cpu_get_pc(space->cpu));
944         return i186.mem.middle_size;
945 
946      case 0x60:
947      case 0x68:
948         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d lower source address\n", cpu_get_pc(space->cpu), (offset - 0x60) / 8);
949         which = (offset - 0x60) / 8;
950//          stream_update(dma_stream, 0);
951         return i186.dma[which].source;
952 
953      case 0x61:
954      case 0x69:
955         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d upper source address\n", cpu_get_pc(space->cpu), (offset - 0x61) / 8);
956         which = (offset - 0x61) / 8;
957//          stream_update(dma_stream, 0);
958         return i186.dma[which].source >> 16;
959 
960      case 0x62:
961      case 0x6a:
962         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d lower dest address\n", cpu_get_pc(space->cpu), (offset - 0x62) / 8);
963         which = (offset - 0x62) / 8;
964//          stream_update(dma_stream, 0);
965         return i186.dma[which].dest;
966 
967      case 0x63:
968      case 0x6b:
969         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d upper dest address\n", cpu_get_pc(space->cpu), (offset - 0x63) / 8);
970         which = (offset - 0x63) / 8;
971//          stream_update(dma_stream, 0);
972         return i186.dma[which].dest >> 16;
973 
974      case 0x64:
975      case 0x6c:
976         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d transfer count\n", cpu_get_pc(space->cpu), (offset - 0x64) / 8);
977         which = (offset - 0x64) / 8;
978//          stream_update(dma_stream, 0);
979         return i186.dma[which].count;
980 
981      case 0x65:
982      case 0x6d:
983         if (LOG_PORTS) logerror("%05X:read 80186 DMA%d control\n", cpu_get_pc(space->cpu), (offset - 0x65) / 8);
984         which = (offset - 0x65) / 8;
985//          stream_update(dma_stream, 0);
986         return i186.dma[which].control;
987 
988      default:
989         logerror("%05X:read 80186 port %02X\n", cpu_get_pc(space->cpu), offset);
990         break;
991   }
992   return 0x00;
993}
994 
995/*************************************
996 *
997 *  80186 internal I/O writes
998 *
999 *************************************/
1000 
1001WRITE16_HANDLER( i186_internal_port_w )
1002{
1003   int temp, which, data16 = data;
1004 
1005   switch (offset)
1006   {
1007      case 0x11:
1008         if (LOG_PORTS) logerror("%05X:80186 EOI = %04X\n", cpu_get_pc(space->cpu), data16);
1009         handle_eoi(space->machine,0x8000);
1010         update_interrupt_state(space->machine);
1011         break;
1012 
1013      case 0x12:
1014         logerror("%05X:ERROR - write to 80186 interrupt poll = %04X\n", cpu_get_pc(space->cpu), data16);
1015         break;
1016 
1017      case 0x13:
1018         logerror("%05X:ERROR - write to 80186 interrupt poll status = %04X\n", cpu_get_pc(space->cpu), data16);
1019         break;
1020 
1021      case 0x14:
1022         if (LOG_PORTS) logerror("%05X:80186 interrupt mask = %04X\n", cpu_get_pc(space->cpu), data16);
1023         i186.intr.timer  = (i186.intr.timer  & ~0x08) | ((data16 << 3) & 0x08);
1024         i186.intr.dma[0] = (i186.intr.dma[0] & ~0x08) | ((data16 << 1) & 0x08);
1025         i186.intr.dma[1] = (i186.intr.dma[1] & ~0x08) | ((data16 << 0) & 0x08);
1026         i186.intr.ext[0] = (i186.intr.ext[0] & ~0x08) | ((data16 >> 1) & 0x08);
1027         i186.intr.ext[1] = (i186.intr.ext[1] & ~0x08) | ((data16 >> 2) & 0x08);
1028         i186.intr.ext[2] = (i186.intr.ext[2] & ~0x08) | ((data16 >> 3) & 0x08);
1029         i186.intr.ext[3] = (i186.intr.ext[3] & ~0x08) | ((data16 >> 4) & 0x08);
1030         update_interrupt_state(space->machine);
1031         break;
1032 
1033      case 0x15:
1034         if (LOG_PORTS) logerror("%05X:80186 interrupt priority mask = %04X\n", cpu_get_pc(space->cpu), data16);
1035         i186.intr.priority_mask = data16 & 0x0007;
1036         update_interrupt_state(space->machine);
1037         break;
1038 
1039      case 0x16:
1040         if (LOG_PORTS) logerror("%05X:80186 interrupt in-service = %04X\n", cpu_get_pc(space->cpu), data16);
1041         i186.intr.in_service = data16 & 0x00ff;
1042         update_interrupt_state(space->machine);
1043         break;
1044 
1045      case 0x17:
1046         if (LOG_PORTS) logerror("%05X:80186 interrupt request = %04X\n", cpu_get_pc(space->cpu), data16);
1047         i186.intr.request = (i186.intr.request & ~0x00c0) | (data16 & 0x00c0);
1048         update_interrupt_state(space->machine);
1049         break;
1050 
1051      case 0x18:
1052         if (LOG_PORTS) logerror("%05X:WARNING - wrote to 80186 interrupt status = %04X\n", cpu_get_pc(space->cpu), data16);
1053         i186.intr.status = (i186.intr.status & ~0x8007) | (data16 & 0x8007);
1054         update_interrupt_state(space->machine);
1055         break;
1056 
1057      case 0x19:
1058         if (LOG_PORTS) logerror("%05X:80186 timer interrupt contol = %04X\n", cpu_get_pc(space->cpu), data16);
1059         i186.intr.timer = data16 & 0x000f;
1060         break;
1061 
1062      case 0x1a:
1063         if (LOG_PORTS) logerror("%05X:80186 DMA 0 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1064         i186.intr.dma[0] = data16 & 0x000f;
1065         break;
1066 
1067      case 0x1b:
1068         if (LOG_PORTS) logerror("%05X:80186 DMA 1 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1069         i186.intr.dma[1] = data16 & 0x000f;
1070         break;
1071 
1072      case 0x1c:
1073         if (LOG_PORTS) logerror("%05X:80186 INT 0 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1074         i186.intr.ext[0] = data16 & 0x007f;
1075         break;
1076 
1077      case 0x1d:
1078         if (LOG_PORTS) logerror("%05X:80186 INT 1 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1079         i186.intr.ext[1] = data16 & 0x007f;
1080         break;
1081 
1082      case 0x1e:
1083         if (LOG_PORTS) logerror("%05X:80186 INT 2 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1084         i186.intr.ext[2] = data16 & 0x001f;
1085         break;
1086 
1087      case 0x1f:
1088         if (LOG_PORTS) logerror("%05X:80186 INT 3 interrupt control = %04X\n", cpu_get_pc(space->cpu), data16);
1089         i186.intr.ext[3] = data16 & 0x001f;
1090         break;
1091 
1092      case 0x28:
1093      case 0x2c:
1094      case 0x30:
1095         if (LOG_PORTS) logerror("%05X:80186 Timer %d count = %04X\n", cpu_get_pc(space->cpu), (offset - 0x28) / 4, data16);
1096         which = (offset - 0x28) / 4;
1097         internal_timer_update(space->machine,which, data16, -1, -1, -1);
1098         break;
1099 
1100      case 0x29:
1101      case 0x2d:
1102      case 0x31:
1103         if (LOG_PORTS) logerror("%05X:80186 Timer %d max A = %04X\n", cpu_get_pc(space->cpu), (offset - 0x29) / 4, data16);
1104         which = (offset - 0x29) / 4;
1105         internal_timer_update(space->machine,which, -1, data16, -1, -1);
1106         break;
1107 
1108      case 0x2a:
1109      case 0x2e:
1110         if (LOG_PORTS) logerror("%05X:80186 Timer %d max B = %04X\n", cpu_get_pc(space->cpu), (offset - 0x2a) / 4, data16);
1111         which = (offset - 0x2a) / 4;
1112         internal_timer_update(space->machine,which, -1, -1, data16, -1);
1113         break;
1114 
1115      case 0x2b:
1116      case 0x2f:
1117      case 0x33:
1118         if (LOG_PORTS) logerror("%05X:80186 Timer %d control = %04X\n", cpu_get_pc(space->cpu), (offset - 0x2b) / 4, data16);
1119         which = (offset - 0x2b) / 4;
1120         internal_timer_update(space->machine,which, -1, -1, -1, data16);
1121         break;
1122 
1123      case 0x50:
1124         if (LOG_PORTS) logerror("%05X:80186 upper chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1125         i186.mem.upper = data16 | 0xc038;
1126         break;
1127 
1128      case 0x51:
1129         if (LOG_PORTS) logerror("%05X:80186 lower chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1130         i186.mem.lower = (data16 & 0x3fff) | 0x0038; printf("%X",i186.mem.lower);
1131         break;
1132 
1133      case 0x52:
1134         if (LOG_PORTS) logerror("%05X:80186 peripheral chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1135         i186.mem.peripheral = data16 | 0x0038;
1136         break;
1137 
1138      case 0x53:
1139         if (LOG_PORTS) logerror("%05X:80186 middle chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1140         i186.mem.middle = data16 | 0x01f8;
1141         break;
1142 
1143      case 0x54:
1144         if (LOG_PORTS) logerror("%05X:80186 middle P chip select = %04X\n", cpu_get_pc(space->cpu), data16);
1145         i186.mem.middle_size = data16 | 0x8038;
1146 
1147         temp = (i186.mem.peripheral & 0xffc0) << 4;
1148         if (i186.mem.middle_size & 0x0040)
1149         {
1150//              install_mem_read_handler(2, temp, temp + 0x2ff, peripheral_r);
1151//              install_mem_write_handler(2, temp, temp + 0x2ff, peripheral_w);
1152         }
1153         else
1154         {
1155            temp &= 0xffff;
1156//              install_port_read_handler(2, temp, temp + 0x2ff, peripheral_r);
1157//              install_port_write_handler(2, temp, temp + 0x2ff, peripheral_w);
1158         }
1159 
1160         /* we need to do this at a time when the I86 context is swapped in */
1161         /* this register is generally set once at startup and never again, so it's a good */
1162         /* time to set it up */
1163         cpu_set_irq_callback(space->cpu, int_callback);
1164         break;
1165 
1166      case 0x60:
1167      case 0x68:
1168         if (LOG_PORTS) logerror("%05X:80186 DMA%d lower source address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x60) / 8, data16);
1169         which = (offset - 0x60) / 8;
1170//          stream_update(dma_stream, 0);
1171         i186.dma[which].source = (i186.dma[which].source & ~0x0ffff) | (data16 & 0x0ffff);
1172         break;
1173 
1174      case 0x61:
1175      case 0x69:
1176         if (LOG_PORTS) logerror("%05X:80186 DMA%d upper source address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x61) / 8, data16);
1177         which = (offset - 0x61) / 8;
1178//          stream_update(dma_stream, 0);
1179         i186.dma[which].source = (i186.dma[which].source & ~0xf0000) | ((data16 << 16) & 0xf0000);
1180         break;
1181 
1182      case 0x62:
1183      case 0x6a:
1184         if (LOG_PORTS) logerror("%05X:80186 DMA%d lower dest address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x62) / 8, data16);
1185         which = (offset - 0x62) / 8;
1186//          stream_update(dma_stream, 0);
1187         i186.dma[which].dest = (i186.dma[which].dest & ~0x0ffff) | (data16 & 0x0ffff);
1188         break;
1189 
1190      case 0x63:
1191      case 0x6b:
1192         if (LOG_PORTS) logerror("%05X:80186 DMA%d upper dest address = %04X\n", cpu_get_pc(space->cpu), (offset - 0x63) / 8, data16);
1193         which = (offset - 0x63) / 8;
1194//          stream_update(dma_stream, 0);
1195         i186.dma[which].dest = (i186.dma[which].dest & ~0xf0000) | ((data16 << 16) & 0xf0000);
1196         break;
1197 
1198      case 0x64:
1199      case 0x6c:
1200         if (LOG_PORTS) logerror("%05X:80186 DMA%d transfer count = %04X\n", cpu_get_pc(space->cpu), (offset - 0x64) / 8, data16);
1201         which = (offset - 0x64) / 8;
1202//          stream_update(dma_stream, 0);
1203         i186.dma[which].count = data16;
1204         break;
1205 
1206      case 0x65:
1207      case 0x6d:
1208         if (LOG_PORTS) logerror("%05X:80186 DMA%d control = %04X\n", cpu_get_pc(space->cpu), (offset - 0x65) / 8, data16);
1209         which = (offset - 0x65) / 8;
1210//          stream_update(dma_stream, 0);
1211         update_dma_control(space->machine, which, data16);
1212         break;
1213 
1214      case 0x7f:
1215         if (LOG_PORTS) logerror("%05X:80186 relocation register = %04X\n", cpu_get_pc(space->cpu), data16);
1216 
1217         /* we assume here there that this doesn't happen too often */
1218         /* plus, we can't really remove the old memory range, so we also assume that it's */
1219         /* okay to leave us mapped where we were */
1220         temp = (data16 & 0x0fff) << 8;
1221         if (data16 & 0x1000)
1222         {
1223            memory_install_read16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_PROGRAM), temp, temp + 0xff, 0, 0, i186_internal_port_r);
1224            memory_install_write16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_PROGRAM), temp, temp + 0xff, 0, 0, i186_internal_port_w);
1225         }
1226         else
1227         {
1228            temp &= 0xffff;
1229            memory_install_read16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_IO), temp, temp + 0xff, 0, 0, i186_internal_port_r);
1230            memory_install_write16_handler(cputag_get_address_space(space->machine, MAINCPU_TAG, ADDRESS_SPACE_IO), temp, temp + 0xff, 0, 0, i186_internal_port_w);
1231         }
1232/*          popmessage("Sound CPU reset");*/
1233         break;
1234 
1235      default:
1236         logerror("%05X:80186 port %02X = %04X\n", cpu_get_pc(space->cpu), offset, data16);
1237         break;
1238   }
1239}
1240 
1241MACHINE_RESET(nimbus)
1242{
1243   /* CPU */
1244   nimbus_cpu_reset(machine); 
1245    fdc_reset();
1246    keyboard_reset();
1247}
1248 
1249DRIVER_INIT(nimbus)
1250{
1251}
1252 
1253MACHINE_START( nimbus )
1254{
1255    /* init cpu */
1256    nimbus_cpu_init(machine);
1257 
1258    keyboard.keyscan_timer=timer_alloc(machine, keyscan_callback, NULL);
1259     
1260   /* setup debug commands */
1261   if (machine->debug_flags & DEBUG_FLAG_ENABLED)
1262   {
1263      debug_console_register_command(machine, "nimbus_irq", CMDFLAG_NONE, 0, 0, 1, execute_debug_irq);
1264        debug_console_register_command(machine, "nimbus_intmasks", CMDFLAG_NONE, 0, 0, 0, execute_debug_intmasks);
1265 
1266        /* set up the instruction hook */
1267        debug_cpu_set_instruction_hook(cputag_get_cpu(machine, MAINCPU_TAG), instruction_hook);
1268     
1269    }
1270}
1271 
1272static void execute_debug_irq(running_machine *machine, int ref, int params, const char *param[])
1273{
1274    int IntNo;
1275    int IntMask;
1276     
1277    if(params>0)
1278    {   
1279        sscanf(param[0],"%d",&IntNo);
1280         
1281        IntMask=0x0000;
1282     
1283        switch (IntNo)
1284        {
1285            case 0  : IntMask=0x010; break;
1286            case 1  : IntMask=0x020; break;
1287            case 2  : IntMask=0x040; break;
1288            case 3  : IntMask=0x080; break;
1289        }
1290         
1291        i186.intr.request |= IntMask;
1292        update_interrupt_state(machine);
1293         
1294        debug_console_printf(machine,"triggering IRQ%d, IntMask=%4.4X, i186.intr.request=%4.4X\n",IntNo,IntMask,i186.intr.request);
1295         
1296//        cputag_set_input_line(machine, MAINCPU_TAG, IntNo, HOLD_LINE );
1297//        cputag_set_input_line(machine, MAINCPU_TAG, IntNo, CLEAR_LINE );
1298         
1299    }
1300    else
1301    {
1302        debug_console_printf(machine,"Error, you must supply an intno to trigger\n");
1303    }
1304   
1305     
1306}
1307 
1308 
1309static void execute_debug_intmasks(running_machine *machine, int ref, int params, const char *param[])
1310{
1311    int IntNo;
1312     
1313     
1314    debug_console_printf(machine,"i186.intr.priority_mask=%4X\n",i186.intr.priority_mask);
1315    for(IntNo=0; IntNo<4; IntNo++)
1316    {
1317        debug_console_printf(machine,"extInt%d mask=%4X\n",IntNo,i186.intr.ext[IntNo]);
1318    }
1319     
1320    debug_console_printf(machine,"i186.intr.request   = %04X\n",i186.intr.request);
1321    debug_console_printf(machine,"i186.intr.ack_mask  = %04X\n",i186.intr.ack_mask);
1322    debug_console_printf(machine,"i186.intr.in_service= %04X\n",i186.intr.in_service);
1323}
1324 
1325/*-----------------------------------------------
1326    instruction_hook - per-instruction hook
1327-----------------------------------------------*/
1328 
1329static int instruction_hook(const device_config *device, offs_t curpc)
1330{
1331    const address_space *space = cpu_get_address_space(device, ADDRESS_SPACE_PROGRAM);
1332    UINT8               *addr_ptr;
1333     
1334    addr_ptr = memory_get_read_ptr(space,curpc);
1335         
1336    if ((addr_ptr !=NULL) && (addr_ptr[0]==0xCD) && (addr_ptr[1]==0xF0))
1337        decode_subbios(device,curpc);
1338     
1339    return 0;
1340}
1341 
1342#define set_type(type_name)     sprintf(type_str,type_name)
1343#define set_drv(drv_name)       sprintf(drv_str,drv_name)
1344#define set_func(func_name)     sprintf(func_str,func_name)
1345 
1346static void decode_subbios(const device_config *device,offs_t pc)
1347{
1348    char    type_str[80];
1349    char    drv_str[80];
1350    char    func_str[80];
1351     
1352    void (*dump_dssi)(const device_config *,UINT16, UINT16) = NULL;
1353 
1354    const device_config *cpu = cputag_get_cpu(device->machine,MAINCPU_TAG);
1355     
1356    UINT16  ax = cpu_get_reg(cpu,I8086_AX);
1357    UINT16  bx = cpu_get_reg(cpu,I8086_BX);
1358    UINT16  cx = cpu_get_reg(cpu,I8086_CX);
1359    UINT16  ds = cpu_get_reg(cpu,I8086_DS);
1360    UINT16  si = cpu_get_reg(cpu,I8086_SI);
1361     
1362    logerror("=======================================================================\n");
1363    logerror("Sub-bios call at %08X, AX=%04X, BX=%04X, CX=%04X, DS:SI=%04X:%04X\n",pc,ax,bx,cx,ds,si);
1364 
1365    set_type("invalid");
1366    set_drv("invalid");
1367    set_func("invalid");
1368     
1369 
1370    switch (cx)
1371    {
1372        case 0   : 
1373        {
1374            set_type("t_mummu");
1375            set_drv("d_mummu");
1376             
1377            switch (ax)
1378            {
1379                case 0  : set_func("f_get_version_number"); break;
1380                case 1  : set_func("f_add_type_code"); break;
1381                case 2  : set_func("f_del_typc_code"); break;
1382                case 3  : set_func("f_get_TCB"); break;
1383                case 4  : set_func("f_add_driver_code"); break;
1384                case 5  : set_func("f_del_driver_code"); break;
1385                case 6  : set_func("f_get_DCB"); break;
1386                case 7  : set_func("f_get_copyright"); break;
1387            }
1388        }; break;
1389         
1390        case 1   : 
1391        {
1392            set_type("t_character");
1393            set_drv("d_printer");
1394             
1395            switch(ax)
1396            {
1397                case 0  : set_func("f_get_version_number"); break;
1398                case 1  : set_func("f_get_output_status"); break;
1399                case 2  : set_func("f_output_character"); break;
1400                case 3  : set_func("f_get_input_status"); break;
1401                case 4  : set_func("f_get_and_remove"); break;
1402                case 5  : set_func("f_get_no_remove"); break;
1403                case 6  : set_func("f_get_last_and_remove"); break;
1404                case 7  : set_func("f_get_last_no_remove"); break;
1405                case 8  : set_func("f_set_IO_parameters"); break;
1406            }
1407        }; break;
1408         
1409        case 2   : 
1410        {
1411            set_type("t_disk");
1412             
1413            switch(bx)
1414            {
1415                case 0  : set_drv("d_floppy"); break;
1416                case 1  : set_drv("d_winchester"); break;
1417                case 2  : set_drv("d_tape"); break;
1418                case 3  : set_drv("d_rompack"); break;
1419                case 4  : set_drv("d_eeprom"); break;
1420            }
1421             
1422            switch(ax)
1423            {
1424                case 0  : set_func("f_get_version_number"); break;             
1425                case 1  : set_func("f_initialise_unit"); break;
1426                case 2  : set_func("f_pseudo_init_unit"); break;
1427                case 3  : set_func("f_get_device_status"); break;
1428                case 4  : set_func("f_read_n_sectors"); break;
1429                case 5  : set_func("f_write_n_sectors"); break;
1430                case 6  : set_func("f_verify_n_sectors"); break;
1431                case 7  : set_func("f_media_check"); break;
1432                case 8  : set_func("f_recalibrate"); break;
1433                case 9  : set_func("f_motors_off"); break;
1434            }
1435             
1436        }; break;
1437         
1438        case 3   : 
1439        {
1440            set_type("t_piconet");
1441            switch(ax)
1442            {
1443                case 0  : set_func("f_get_version_number"); break;             
1444            }
1445        }; break;
1446         
1447        case 4   : 
1448        {
1449            set_type("t_tick");
1450            set_drv("d_tick");
1451             
1452            switch(ax)
1453            {
1454                case 0  : set_func("f_get_version_number"); break; 
1455                case 1  : set_func("f_ticks_per_second"); break; 
1456                case 2  : set_func("f_link_tick_routine"); break; 
1457                case 3  : set_func("f_unlink_tick_routine"); break;             
1458            }
1459        }; break;
1460         
1461        case 5   : 
1462        {
1463            set_type("t_graphics_input");
1464            switch(ax)
1465            {
1466                case 0  : set_func("f_get_version_number"); break;             
1467            }
1468        }; break;
1469         
1470        case 6   : 
1471        {
1472            set_type("t_graphics_output");
1473            set_drv("d_ngc_screen");
1474 
1475            switch(ax)
1476            {
1477                case 0  : set_func("f_get_version_number");                 break; 
1478                case 1  : set_func("f_graphics_output_cold_start");         break; 
1479                case 2  : set_func("f_graphics_output_warm_start");         break; 
1480                case 3  : set_func("f_graphics_output_off");                break; 
1481                case 4  : set_func("f_reinit_graphics_output");             break; 
1482                case 5  : set_func("f_polymarker");                         break; 
1483                case 6  : set_func("f_polyline");                           break; 
1484                case 7  : set_func("f_fill_area"); dump_dssi=&decode_dssi_f_fill_area; break; 
1485                case 8  : set_func("f_flood_fill_area"); break; 
1486                case 9  : set_func("f_plot_character_string"); dump_dssi=&decode_dssi_f_plot_character_string; break; 
1487                case 10 : set_func("f_define_graphics_clipping_area"); break; 
1488                case 11 : set_func("f_enquire_clipping_area_limits"); break; 
1489                case 12 : set_func("f_select_graphics_clipping_area"); break; 
1490                case 13 : set_func("f_enq_selctd_graphics_clip_area"); break; 
1491                case 14 : set_func("f_set_clt_element"); break; 
1492                case 15 : set_func("f_enquire_clt_element"); break; 
1493                case 16 : set_func("f_set_new_clt"); dump_dssi=&decode_dssi_f_set_new_clt; break; 
1494                case 17 : set_func("f_enquire_clt_contents"); break; 
1495                case 18 : set_func("f_define_dithering_pattern"); break; 
1496                case 19 : set_func("f_enquire_dithering_pattern"); break; 
1497                case 20 : set_func("f_draw_sprite"); break; 
1498                case 21 : set_func("f_move_sprite"); break; 
1499                case 22 : set_func("f_erase_sprite"); break; 
1500                case 23 : set_func("f_read_pixel"); break; 
1501                case 24 : set_func("f_read_to_limit"); break; 
1502                case 25 : set_func("f_read_area_pixel"); break; 
1503                case 26 : set_func("f_write_area_pixel"); break; 
1504                case 27 : set_func("f_copy_area_pixel"); break; 
1505 
1506                case 29 : set_func("f_read_area_word"); break; 
1507                case 30 : set_func("f_write_area_word"); break; 
1508                case 31 : set_func("f_copy_area_word"); break; 
1509                case 32 : set_func("f_swap_area_word"); break; 
1510                case 33 : set_func("f_set_border_colour"); break; 
1511                case 34 : set_func("f_enquire_border_colour"); break; 
1512                case 35 : set_func("f_enquire_miscellaneous_data"); break; 
1513                case 36  : set_func("f_circle"); break; 
1514 
1515                case 38 : set_func("f_arc_of_ellipse"); break; 
1516                case 39 : set_func("f_isin"); break; 
1517                case 40 : set_func("f_icos"); break; 
1518                case 41 : set_func("f_define_hatching_pattern"); break; 
1519                case 42 : set_func("f_enquire_hatching_pattern"); break; 
1520                case 43 : set_func("f_enquire_display_line"); break; 
1521                case 44 : set_func("f_plonk_logo"); break; 
1522            }
1523        }; break;
1524         
1525        case 7   : 
1526        {
1527            set_type("t_zend");
1528            switch(ax)
1529            {
1530                case 0  : set_func("f_get_version_number"); break;             
1531            }
1532        }; break;
1533         
1534        case 8   : 
1535        {
1536            set_type("t_zep");
1537            switch(ax)
1538            {
1539                case 0  : set_func("f_get_version_number"); break;             
1540            }
1541        }; break;
1542         
1543        case 9   : 
1544        {
1545            set_type("t_raw_console");
1546             
1547            switch(bx)
1548            {
1549                case 0  : 
1550                {
1551                    set_drv("d_screen");
1552             
1553                    switch(ax)
1554                    {
1555                        case 0  : set_func("f_get_version_number"); break;             
1556                        case 1  : set_func("f_plonk_char"); dump_dssi=decode_dssi_f_plonk_char; break;
1557                        case 2  : set_func("f_plonk_cursor"); break;
1558                        case 3  : set_func("f_kill_cursor"); break;
1559                        case 4  : set_func("f_scroll"); break;
1560                        case 5  : set_func("f_width"); break;
1561                        case 6  : set_func("f_get_char_set"); break;
1562                        case 7  : set_func("f_set_char_set"); break;
1563                        case 8  : set_func("f_reset_char_set"); break;
1564                        case 9  : set_func("f_set_plonk_parameters"); break;
1565                        case 10 : set_func("f_set_cursor_flash_rate"); break;
1566                    }
1567                }; break;
1568             
1569                case 1  :
1570                {
1571                    set_drv("d_keyboard");
1572                     
1573                    switch(ax)
1574                    {
1575                        case 0  : set_func("f_get_version_number"); break;             
1576                        case 1  : set_func("f_init_keyboard"); break;
1577                        case 2  : set_func("f_get_last_key_code"); break;
1578                        case 3  : set_func("f_get_bitmap"); break;
1579                    }
1580                }; break;
1581            }
1582        }; break;
1583         
1584        case 10   : 
1585        {
1586            set_type("t_acoustics");
1587            switch(ax)
1588            {
1589                case 0  : set_func("f_get_version_number"); break;             
1590            }
1591        }; break;
1592         
1593        case 11   : 
1594        {
1595            set_type("t_hard_sums");
1596            switch(ax)
1597            {
1598                case 0  : set_func("f_get_version_number"); break;             
1599            }
1600        }; break;     
1601    }
1602     
1603    logerror("Type=%s, Driver=%s, Function=%s\n",type_str,drv_str,func_str);
1604     
1605    if(dump_dssi!=NULL)
1606        dump_dssi(device,ds,si);
1607    logerror("=======================================================================\n");
1608}
1609 
1610void *get_dssi_ptr(const address_space *space, UINT16   ds, UINT16 si)
1611{
1612    int             addr;
1613 
1614    addr=((ds<<4)+si);
1615    OUTPUT_SEGOFS("DS:SI",ds,si);
1616 
1617    return memory_get_read_ptr(space, addr);
1618}
1619 
1620static void decode_dssi_f_fill_area(const device_config *device,UINT16  ds, UINT16 si)
1621{
1622    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1623 
1624    UINT16          *addr_ptr;
1625    t_area_params   *area_params;
1626    t_nimbus_brush  *brush;
1627    int             cocount;
1628 
1629    area_params = get_dssi_ptr(space,ds,si);
1630     
1631    OUTPUT_SEGOFS("SegBrush:OfsBrush",area_params->seg_brush,area_params->ofs_brush);
1632    brush=memory_get_read_ptr(space, LINEAR_ADDR(area_params->seg_brush,area_params->ofs_brush));
1633     
1634    logerror("Brush params\n");
1635    logerror("Style=%04X,          StyleIndex=%04X\n",brush->style,brush->style_index);
1636    logerror("Colour1=%04X,        Colour2=%04X\n",brush->colour1,brush->colour2);
1637    logerror("transparency=%04X,   boundry_spec=%04X\n",brush->transparency,brush->boundary_spec);
1638    logerror("boundry colour=%04X, save colour=%04X\n",brush->boundary_colour,brush->save_colour);
1639             
1640     
1641    OUTPUT_SEGOFS("SegData:OfsData",area_params->seg_data,area_params->ofs_data);
1642     
1643    addr_ptr = memory_get_read_ptr(space, LINEAR_ADDR(area_params->seg_data,area_params->ofs_data));
1644    for(cocount=0; cocount < area_params->count; cocount++)
1645    {
1646        logerror("x=%d y=%d\n",addr_ptr[cocount*2],addr_ptr[(cocount*2)+1]);
1647    }
1648}
1649 
1650static void decode_dssi_f_plot_character_string(const device_config *device,UINT16  ds, UINT16 si)
1651{
1652    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1653 
1654    UINT8          *char_ptr;
1655    t_plot_string_params   *plot_string_params;
1656    int             charno;
1657       
1658    plot_string_params=get_dssi_ptr(space,ds,si);
1659     
1660    OUTPUT_SEGOFS("SegFont:OfsFont",plot_string_params->seg_font,plot_string_params->ofs_font);
1661    OUTPUT_SEGOFS("SegData:OfsData",plot_string_params->seg_data,plot_string_params->ofs_data);
1662     
1663    logerror("x=%d, y=%d, length=%d\n",plot_string_params->x,plot_string_params->y,plot_string_params->length);
1664     
1665    char_ptr=memory_get_read_ptr(space, LINEAR_ADDR(plot_string_params->seg_data,plot_string_params->ofs_data));
1666 
1667    if (plot_string_params->length==0xFFFF)
1668        logerror("%s",char_ptr);
1669    else
1670        for(charno=0;charno<plot_string_params->length;charno++)
1671            logerror("%c",char_ptr[charno]);
1672         
1673    logerror("\n");
1674}
1675 
1676static void decode_dssi_f_set_new_clt(const device_config *device,UINT16  ds, UINT16 si)
1677{
1678    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1679    UINT16  *new_colours;
1680    int     colour;
1681    new_colours=get_dssi_ptr(space,ds,si);
1682     
1683    OUTPUT_SEGOFS("SegColours:OfsColours",ds,si);
1684     
1685    for(colour=0;colour<16;colour++)
1686        logerror("colour #%02X=%04X\n",colour,new_colours[colour]);
1687     
1688}
1689 
1690static void decode_dssi_f_plonk_char(const device_config *device,UINT16  ds, UINT16 si)
1691{
1692    const address_space *space = cputag_get_address_space(device->machine,MAINCPU_TAG, ADDRESS_SPACE_PROGRAM);
1693    UINT16  *params;
1694    params=get_dssi_ptr(space,ds,si);
1695     
1696    OUTPUT_SEGOFS("SegParams:OfsParams",ds,si);
1697 
1698    logerror("plonked_char=%c\n",params[0]);
1699}
1700 
1701 
1702READ16_HANDLER( nimbus_io_r )
1703{
1704    int pc=cpu_get_pc(space->cpu);
1705     
1706    logerror("Nimbus IOR at pc=%08X from %04X mask=%04X, data=%04X\n",pc,(offset*2)+0x30,mem_mask,IOPorts[offset]);
1707     
1708    switch (offset*2)
1709    {
1710        default         : return IOPorts[offset]; break;
1711    }
1712    return 0;
1713}
1714 
1715WRITE16_HANDLER( nimbus_io_w )
1716{
1717    int pc=cpu_get_pc(space->cpu);
1718 
1719    logerror("Nimbus IOW at %08X write of %04X to %04X mask=%04X\n",pc,data,(offset*2)+0x30,mem_mask);
1720     
1721    switch (offset*2)
1722    {
1723        default         : COMBINE_DATA(&IOPorts[offset]); break;
1724    }
1725}
1726 
1727 
1728/* 
1729    Keyboard emulation 
1730     
1731*/
1732 
1733static void keyboard_reset(void)
1734{
1735    memset(keyboard.keyrows,0xFF,NIMBUS_KEYROWS);
1736     
1737    // Setup timer to scan keyboard.
1738    timer_adjust_periodic(keyboard.keyscan_timer, attotime_zero, 0, ATTOTIME_IN_HZ(50));
1739 
1740}
1741 
1742static void queue_scancode(UINT8 scancode)
1743{
1744   keyboard.queue[keyboard.head] = scancode;
1745   keyboard.head++;
1746   keyboard.head %= ARRAY_LENGTH(keyboard.queue);
1747}
1748 
1749static int keyboard_queue_read(void)
1750{
1751   int data;
1752   if (keyboard.tail == keyboard.head)
1753      return -1;
1754 
1755   data = keyboard.queue[keyboard.tail];
1756 
1757   if (LOG_KEYBOARD)
1758      logerror("keyboard_queue_read(): Keyboard Read 0x%02x\n",data);
1759 
1760   keyboard.tail++;
1761   keyboard.tail %= ARRAY_LENGTH(keyboard.queue);
1762   return data;
1763}
1764 
1765static TIMER_CALLBACK(keyscan_callback)
1766{
1767    UINT8   keyrow;
1768    UINT8   row;
1769    UINT8   bitno;
1770    UINT8   mask;
1771    static const char *const keynames[] = { "KEY0", "KEY1", "KEY2", "KEY3", "KEY4",
1772                                             "KEY5", "KEY6", "KEY7", "KEY8", "KEY9", 
1773                                             "KEY10"};
1774 
1775    for(row=0;row<NIMBUS_KEYROWS;row++)
1776    {
1777        keyrow=input_port_read(machine, keynames[row]);
1778         
1779        for(mask=0x80, bitno=7;mask>0;mask=mask>>1, bitno-=1)
1780        {
1781            if(!(keyrow & mask) && (keyboard.keyrows[row] & mask))
1782            {
1783                if (LOG_KEYBOARD) logerror("keypress %02X\n",(row<<3)+bitno);
1784                queue_scancode((row<<3)+bitno);
1785            }
1786   
1787            if((keyrow & mask) && !(keyboard.keyrows[row] & mask))
1788            {
1789                if (LOG_KEYBOARD) logerror("keyrelease %02X\n",0x80+(row<<3)+bitno);   
1790                queue_scancode(0x80+(row<<3)+bitno);
1791            }
1792        }
1793         
1794        keyboard.keyrows[row]=keyrow;
1795    }
1796}
1797 
1798 
1799/* 
1800 
1801Z80SIO, used for the keyboard interface 
1802 
1803*/
1804 
1805READ8_DEVICE_HANDLER( sio_r )
1806{
1807    int pc=cpu_get_pc(cputag_get_cpu(device->machine,MAINCPU_TAG));
1808    UINT8 result = 0;
1809     
1810   switch (offset*2)
1811   {
1812        case 0 :
1813            result=z80sio_d_r(device, 0); 
1814            break;
1815        case 2 :
1816            result=z80sio_d_r(device, 1); 
1817            break;
1818        case 4 :
1819            result=z80sio_c_r(device, 0);
1820            break;
1821        case 6 :
1822            result=z80sio_c_r(device, 1);
1823            break;
1824   }
1825 
1826    if(LOG_SIO)
1827        logerror("Nimbus SIOR at pc=%08X from %04X data=%02X\n",pc,(offset*2)+0xF0,result);
1828 
1829   return result;
1830}
1831 
1832WRITE8_DEVICE_HANDLER( sio_w )
1833{
1834    int pc=cpu_get_pc(cputag_get_cpu(device->machine,MAINCPU_TAG));
1835 
1836    if(LOG_SIO)
1837        logerror("Nimbus SIOW at %08X write of %02X to %04X\n",pc,data,(offset*2)+0xF0);
1838 
1839   switch (offset*2)
1840   {
1841        case 0 :
1842            z80sio_d_w(device, 0, data);
1843            break;
1844        case 2 :
1845            z80sio_d_w(device, 1, data);
1846            break;
1847        case 4 :
1848            z80sio_c_w(device, 0, data);
1849            break;
1850        case 5 :
1851            z80sio_c_w(device, 1, data);
1852            break;
1853   }
1854}
1855 
1856/* Z80 SIO/2 */
1857 
1858void sio_interrupt(const device_config *device, int state)
1859{
1860    if(LOG_SIO)
1861        logerror("SIO Interrupt state=%02X\n",state);
1862 
1863    // Don't re-trigger if already active !
1864    if(state!=sio_int_state)
1865    {
1866        sio_int_state=state;
1867         
1868        if(state)
1869            external_int(device->machine,0,0x9C);
1870    }
1871}
1872 
1873WRITE8_DEVICE_HANDLER( sio_dtr_w )
1874{
1875   if (offset == 1)
1876   {
1877   }
1878}
1879 
1880WRITE8_DEVICE_HANDLER( sio_serial_transmit )
1881{
1882}
1883 
1884int sio_serial_receive( const device_config *device, int channel )
1885{
1886   if(channel==0)
1887    {
1888        return keyboard_queue_read();       
1889    }
1890    else
1891        return -1;
1892}
1893 
1894/* Floppy disk */
1895 
1896static void fdc_reset(void)
1897{
1898    nimbus_fdc.irq_enabled=0;
1899    nimbus_fdc.drq_enabled=0;
1900    nimbus_fdc.diskno=0;
1901    nimbus_fdc.motor_on=0;
1902}
1903 
1904static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_intrq_w )
1905{
1906    if(LOG_DISK)
1907        logerror("nimbus_fdc_intrq_w(%d)\n", state);
1908 
1909    if(state && nimbus_fdc.irq_enabled)
1910    {
1911        external_int(device->machine,0,0x80);
1912    } 
1913}
1914 
1915static WRITE_LINE_DEVICE_HANDLER( nimbus_fdc_drq_w )
1916{
1917 
1918    if(LOG_DISK)
1919        logerror("nimbus_fdc_drq_w(%d)\n", state);
1920     
1921    if(state && nimbus_fdc.drq_enabled)
1922        drq_callback(device->machine,1);
1923}
1924 
1925READ8_HANDLER( nimbus_fdc_r )
1926{
1927      int result = 0;
1928   const device_config *fdc = devtag_get_device(space->machine, FDC_TAG);
1929    int pc=cpu_get_pc(space->cpu);
1930    const device_config *drive = devtag_get_device(space->machine, nimbus_wd17xx_interface.floppy_drive_tags[nimbus_fdc.diskno]);
1931   
1932    switch(offset*2)
1933   {
1934      case 0x08 :
1935         result = wd17xx_status_r(fdc, 0);
1936         if (LOG_DISK) logerror("Disk status=%2.2X\n",result);
1937         break;
1938      case 0x0A :
1939         result = wd17xx_track_r(fdc, 0);
1940         break;
1941      case 0x0C :
1942         result = wd17xx_sector_r(fdc, 0);
1943         break;
1944      case 0x0E :
1945         result = wd17xx_data_r(fdc, 0);
1946         break;
1947        case 0x10 :
1948            result = (nimbus_fdc.motor_on                                    ? 0x04 : 0x00) |
1949                     (floppy_drive_get_flag_state(drive, FLOPPY_DRIVE_INDEX) ? 0x00 : 0x02) |
1950                     (floppy_drive_get_flag_state(drive, FLOPPY_DRIVE_READY) ? 0x01 : 0x00);       
1951            break;
1952      default:
1953         break;
1954   }
1955 
1956    if(LOG_DISK)
1957        logerror("Nimbus FDCR at pc=%08X from %04X data=%02X\n",pc,(offset*2)+0x400,result);
1958 
1959   return result;
1960}
1961 
1962WRITE8_HANDLER( nimbus_fdc_w )
1963{
1964   const device_config *fdc = devtag_get_device(space->machine, FDC_TAG);
1965    int pc=cpu_get_pc(space->cpu);
1966 
1967    if(LOG_DISK)
1968        logerror("Nimbus FDCW at %08X write of %02X to %04X\n",pc,data,(offset*2)+0x400);
1969 
1970    switch(offset*2)
1971   {
1972        case 0x00 :
1973            switch (data & 0x0F)
1974            {
1975                case 0x01 : nimbus_fdc.diskno=0; break;
1976                case 0x02 : nimbus_fdc.diskno=1; break;
1977                case 0x04 : nimbus_fdc.diskno=2; break;
1978                case 0x08 : nimbus_fdc.diskno=3; break;   
1979                //default : nimbus_fdc.diskno=NO_DRIVE_SELECTED; break; // no drive selected
1980            }
1981            if (nimbus_fdc.diskno!=NO_DRIVE_SELECTED)
1982            {
1983                wd17xx_set_drive(fdc, nimbus_fdc.diskno);
1984                wd17xx_set_side(fdc, (data & 0x10) ? 1 : 0);
1985            }
1986            nimbus_fdc.motor_on=(data & 0x20);
1987            nimbus_fdc.drq_enabled=(data & 0x80);
1988            nimbus_fdc.irq_enabled=1;
1989             
1990            // Nimbus FDC is hard wired for double density
1991            wd17xx_set_density(fdc, DEN_MFM_LO);
1992         break;
1993      case 0x08 :
1994         wd17xx_command_w(fdc, 0, data);
1995         break;
1996      case 0x0A :
1997         wd17xx_track_w(fdc, 0, data);
1998         break;
1999      case 0x0C :
2000         wd17xx_sector_w(fdc, 0, data);
2001         break;
2002      case 0x0E :
2003         wd17xx_data_w(fdc, 0, data);
2004         break;
2005   }
2006}
2007 
src/mess/drivers/rmnimbus.c
r6831r6832
6969static INPUT_PORTS_START( nimbus )
7070   PORT_START("KEY0") /* Key row 0 scancodes 00..07 */
7171   //PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("1") PORT_CODE(KEYCODE_1) PORT_CHAR('1')
72    PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_UNUSED)
7273   PORT_BIT(0x02, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("ESC")    PORT_CODE(KEYCODE_ESC)          PORT_CHAR(0x1B)
7374   PORT_BIT(0x04, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("1")      PORT_CODE(KEYCODE_1)            PORT_CHAR('1')
7475   PORT_BIT(0x08, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("2")      PORT_CODE(KEYCODE_2)            PORT_CHAR('2')
r6831r6832
115116   PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("J")      PORT_CODE(KEYCODE_J)            PORT_CHAR('J')
116117   PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("K")      PORT_CODE(KEYCODE_K)            PORT_CHAR('K')
117118   PORT_BIT(0x40, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("L")      PORT_CODE(KEYCODE_L)            PORT_CHAR('L')
118    PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME(";")      PORT_CODE(KEYCODE_QUOTE)        PORT_CHAR(';')
119    PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME(";")      PORT_CODE(KEYCODE_COLON)        PORT_CHAR(';')
119120 
120121   PORT_START("KEY5") /* Key row 5 scancodes 28..2F */
121122    PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("#")      PORT_CODE(KEYCODE_QUOTE)        PORT_CHAR('#')
r6831r6832
160161    PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP7")    PORT_CODE(KEYCODE_7_PAD)        PORT_CHAR('7')
161162 
162163   PORT_START("KEY9") /* Key row 9 scancodes 48..4F */
163   PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP8")    PORT_CODE(KEYCODE_8_PAD)        PORT_CHAR('8')
164   PORT_BIT(0x02, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP9")    PORT_CODE(KEYCODE_9_PAD)        PORT_CHAR('9')
165   PORT_BIT(0x04, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP-")    PORT_CODE(KEYCODE_MINUS_PAD)    PORT_CHAR('-')
166   PORT_BIT(0x08, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP4")    PORT_CODE(KEYCODE_4_PAD)        PORT_CHAR('4')
167   PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP5")    PORT_CODE(KEYCODE_5_PAD)        PORT_CHAR('5')
168   PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP6")    PORT_CODE(KEYCODE_6_PAD)        PORT_CHAR('6')
169   PORT_BIT(0x40, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP+")    PORT_CODE(KEYCODE_PLUS_PAD)     PORT_CHAR('+')
170    PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP1")    PORT_CODE(KEYCODE_1_PAD)        PORT_CHAR('1')
164   PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP8")    PORT_CODE(KEYCODE_8_PAD)        //PORT_CHAR('8')
165   PORT_BIT(0x02, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP9")    PORT_CODE(KEYCODE_9_PAD)        //PORT_CHAR('9')
166   PORT_BIT(0x04, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP-")    PORT_CODE(KEYCODE_MINUS_PAD)    //PORT_CHAR('-')
167   PORT_BIT(0x08, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP4")    PORT_CODE(KEYCODE_4_PAD)        //PORT_CHAR('4')
168   PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP5")    PORT_CODE(KEYCODE_5_PAD)        //PORT_CHAR('5')
169   PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP6")    PORT_CODE(KEYCODE_6_PAD)        //PORT_CHAR('6')
170   PORT_BIT(0x40, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP+")    PORT_CODE(KEYCODE_PLUS_PAD)     //PORT_CHAR('+')
171    PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP1")    PORT_CODE(KEYCODE_1_PAD)        //PORT_CHAR('1')
171172 
172173   PORT_START("KEY10") /* Key row 10 scancodes 50..57 */
173   PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP2")    PORT_CODE(KEYCODE_2_PAD)        PORT_CHAR('2')
174   PORT_BIT(0x02, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP3")    PORT_CODE(KEYCODE_3_PAD)        PORT_CHAR('3')
175   PORT_BIT(0x04, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP0")    PORT_CODE(KEYCODE_0_PAD)        PORT_CHAR('0')
176   PORT_BIT(0x08, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP.")    PORT_CODE(KEYCODE_DEL_PAD)      PORT_CHAR('.')
177   //PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP5")    PORT_CODE(KEYCODE_5_PAD)        PORT_CHAR('5')
174   PORT_BIT(0x01, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP2")    PORT_CODE(KEYCODE_2_PAD)        //PORT_CHAR('2')
175   PORT_BIT(0x02, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP3")    PORT_CODE(KEYCODE_3_PAD)        //PORT_CHAR('3')
176   PORT_BIT(0x04, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP0")    PORT_CODE(KEYCODE_0_PAD)        //PORT_CHAR('0')
177   PORT_BIT(0x08, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP.")    PORT_CODE(KEYCODE_DEL_PAD)      //PORT_CHAR('.')
178   PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_UNUSED)
179   PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_UNUSED)
180   PORT_BIT(0x40, IP_ACTIVE_LOW, IPT_UNUSED)
181   PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_UNUSED)
182    //PORT_BIT(0x10, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP5")    PORT_CODE(KEYCODE_5_PAD)        PORT_CHAR('5')
178183   //PORT_BIT(0x20, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP6")    PORT_CODE(KEYCODE_6_PAD)        PORT_CHAR('6')
179184   //PORT_BIT(0x40, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP+")    PORT_CODE(KEYCODE_PLUS_PAD)     PORT_CHAR('+')
180185    //PORT_BIT(0x80, IP_ACTIVE_LOW, IPT_KEYBOARD) PORT_NAME("KP1")    PORT_CODE(KEYCODE_1_PAD)        PORT_CHAR('1')
r6831r6832
219224    MDRV_VIDEO_START(nimbus)
220225    MDRV_VIDEO_UPDATE(nimbus)
221226    MDRV_VIDEO_EOF(nimbus)
227    MDRV_VIDEO_RESET(nimbus)
222228 
223229 
224230   MDRV_WD2793_ADD(FDC_TAG, nimbus_wd17xx_interface )

Previous 15994 Revisions Next


© 1998-2018 The MESS Team