Previous 15994 Revisions Next

r3407 Tuesday 7th October, 2008 at 22:16:18 UTC by Fabio Priuli
[LYNX] Refactored code, cleaning up small bits here and there. Added correct signed math and everon support to Suzy chip.
[src/mess/drivers]lynx.c
[src/mess/includes]lynx.h
[src/mess/machine]lynx.c

src/mess/includes/lynx.h
r3406r3407
88#define LYNX_H_
99
1010#include "sound/custom.h"
11#include "devices/cartslot.h"
1112
1213
13/*----------- defined in drivers/lynx.c -----------*/
14#define LYNX_CART      0
15#define LYNX_QUICKLOAD   1
1416
15void lynx_partialhash(char *dest, const unsigned char *data,
16   unsigned long length, unsigned int functions);
1717
18extern int lynx_rotate;
19
20
21extern UINT32 lynx_palette[0x10];
22
23void lynx_draw_lines(running_machine *machine, int newline);
24
25
26#define PAD_UP 0x80
27#define PAD_DOWN 0x40
28#define PAD_LEFT 0x20
29#define PAD_RIGHT 0x10
30
31
3218/*----------- defined in machine/lynx.c -----------*/
3319
3420MACHINE_START( lynx );
r3406r3407
4026extern UINT8 *lynx_mem_fffa;
4127extern size_t lynx_mem_fe00_size;
4228
43extern UINT16 lynx_granularity;
44
45typedef struct {
46   UINT8 data[0x100];
47} MIKEY;
48
49extern MIKEY mikey;
5029READ8_HANDLER( lynx_memory_config_r );
5130WRITE8_HANDLER( lynx_memory_config_w );
5231WRITE8_HANDLER(mikey_write);
r3406r3407
5534READ8_HANDLER(suzy_read);
5635void lynx_timer_count_down(running_machine *machine, int nr);
5736
37INTERRUPT_GEN( lynx_frame_int );
5838
39/* These functions are also needed for the Quickload */
40int lynx_verify_cart (char *header, int kind);
41void lynx_crc_keyword(const device_config *image);
42
43void lynx_cartslot_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info);
44
45
5946/*----------- defined in audio/lynx.c -----------*/
6047
6148void lynx_audio_reset(void);
r3406r3407
6754
6855
6956#endif /* LYNX_H_ */
70
71
72/* TODO: Move those functions to their own file */
73
74#ifdef INCLUDE_LYNX_LINE_FUNCTION
75   int j, xi, wi, i;
76   int b, p, color;
77
78   i=blitter.mem[blitter.bitmap];
79   blitter.memory_accesses++;
80   for (xi=blitter.x, p=0, b=0, j=1, wi=0; (j<i);) {
81       if (p<bits) {
82      b=(b<<8)|blitter.mem[blitter.bitmap+j];
83      j++;
84      p+=8;
85      blitter.memory_accesses++;
86       }
87       for (;(p>=bits);) {
88      color=blitter.color[(b>>(p-bits))&mask]; p-=bits;
89      for (;(wi<blitter.width);wi+=0x100, xi+=xdir) {
90          if ((xi>=0)&&(xi<160)) {
91         lynx_plot_pixel(blitter.mode, xi, y, color);
92          }
93      }
94      wi-=blitter.width;
95       }
96   }
97#endif
98
99#ifdef INCLUDE_LYNX_LINE_RLE_FUNCTION
100
101   int wi, xi;
102   int b, p, j;
103   int t, count, color;
104
105   for( p=0, j=0, b=0, xi=blitter.x, wi=0; ; ) { // through the rle entries
106       if (p<5+bits) { // under 7 bits no complete entry
107      j++;
108      if (j>=blitter.mem[blitter.bitmap]) return;
109      p+=8;
110      b=(b<<8)|blitter.mem[blitter.bitmap+j];
111      blitter.memory_accesses++;
112       }
113       t=(b>>(p-1))&1;p--;
114       count=((b>>(p-4))&0xf)+1;p-=4;
115       if (t) { // count of different pixels
116      for (;count; count--) {
117          if (p<bits) {
118         j++;
119         if (j>=blitter.mem[blitter.bitmap]) return;
120         p+=8;
121         b=(b<<8)|blitter.mem[blitter.bitmap+j];
122         blitter.memory_accesses++;
123          }
124          color=blitter.color[(b>>(p-bits))&mask];p-=bits;
125          for (;(wi<blitter.width);wi+=0x100, xi+=xdir) {
126         if ((xi>=0)&&(xi<160)) {
127             lynx_plot_pixel(blitter.mode, xi, y, color);
128         }
129          }
130          wi-=blitter.width;
131      }
132       } else { // count of same pixels
133      if (count==0) return;
134      if (p<bits) {
135          j++;
136          if (j>=blitter.mem[blitter.bitmap]) return;
137          p+=8;
138          b=(b<<8)|blitter.mem[blitter.bitmap+j];
139          blitter.memory_accesses++;
140      }
141      color=blitter.color[(b>>(p-bits))&mask];p-=bits;
142      for (;count; count--) {
143          for (;(wi<blitter.width);wi+=0x100, xi+=xdir) {
144         if ((xi>=0)&&(xi<160)) {
145             lynx_plot_pixel(blitter.mode, xi, y, color);
146         }
147          }
148          wi-=blitter.width;
149      }
150       }
151   }
152
153#endif
src/mess/machine/lynx.c
r3406r3407
66#include "includes/lynx.h"
77#include "cpu/m6502/m6502.h"
88
9#include "devices/cartslot.h"
10#include "hash.h"
911
10UINT16 lynx_granularity = 1;
12
13static UINT16 lynx_granularity = 1;
1114static int lynx_line;
15static int lynx_line_y;   
16static int sign_AB = 0, sign_CD = 0;
1217
18static UINT32 lynx_palette[0x10];
19
20static int rotate = 0;
21static int lynx_rotate;
22
23#define MATH_A      0x55
24#define MATH_B      0x54
25#define MATH_C      0x53
26#define MATH_D      0x52
27#define MATH_E      0x63
28#define MATH_F      0x62
29#define MATH_G      0x61
30#define MATH_H      0x60
31#define MATH_J      0x6f
32#define MATH_K      0x6e
33#define MATH_L      0x6d
34#define MATH_M      0x6c
35#define MATH_N      0x57
36#define MATH_P      0x56
37#define SPRG0      0x91
38#define SPRSYS      0x92
39
40
41#define PAD_UP      0x80
42#define PAD_DOWN   0x40
43#define PAD_LEFT   0x20
44#define PAD_RIGHT   0x10
45
1346typedef struct
1447{
15    union
16   {
17      UINT8 data[0x100];
18      struct
19      {
20         struct { UINT8 l, h; }
21         // eng used by the blitter engine
22         // scb written by the blitter scb blocks to engine expander
23         //     used by the engine
24         //     might be used directly by software!
25         eng1, eng2,
26         h_offset, v_offset, vidbas, colbas,
27         eng3, eng4,
28         scb1, scb2, scb3, scb4, scb5, scb6,
29         eng5, eng6, eng7, eng8,
30         colloff,
31         eng9,
32         hsizoff, vsizoff,
33         eng10, eng11;
34         UINT8 res[0x20];
35         UINT8 used[2];
36         UINT8 D,C,B,A,P,N;
37         UINT8 res1[8];
38         UINT8 H,G,F,E;
39         UINT8 res2[8];
40         UINT8 M,L,K,J;
41         UINT8 res3[0x21];
42         UINT8 SPRG0;
43         UINT8 SPRSYS;
44      } s;
45    } u;
46    int accumulate_overflow;
47    UINT8 high;
48    int low;
48   UINT8 data[0x100];
49   int accumulate_overflow;
50   UINT8 high;
51   int low;
4952} SUZY;
5053
5154static SUZY suzy;
5255
5356static struct
5457{
55    UINT8 *mem;
56    // global
57    UINT16 screen;
58    UINT16 colbuf;
59    UINT16 colpos; // byte where value of collision is written
60    UINT16 xoff, yoff;
61    // in command
62    int mode;
63    UINT16 cmd;
64    UINT8 spritenr;
65    int x,y;
66    UINT16 width, height; // uint16 important for blue lightning
67    UINT16 stretch, tilt; // uint16 important
68    UINT8 color[16]; // or stored
69    void (*line_function)(const int y, const int xdir);
70    UINT16 bitmap;
58   UINT8 *mem;
59   // global
60   UINT16 screen;
61   UINT16 colbuf;
62   UINT16 colpos; // byte where value of collision is written
63   UINT16 xoff, yoff;
64   // in command
65   int mode;
66   UINT16 cmd;
67   UINT8 spritenr;
68   int x,y;
69   UINT16 width, height; // uint16 important for blue lightning
70   UINT16 stretch, tilt; // uint16 important
71   UINT8 color[16]; // or stored
72   void (*line_function)(const int y, const int xdir);
73   UINT16 bitmap;
7174
72    int everon;
73    int memory_accesses;
74    attotime time;
75   int everon;
76   int memory_accesses;
77   attotime time;
7578} blitter;
7679
7780UINT8 *lynx_mem_0000;
r3406r3407
8386
8487static UINT8 lynx_memory_config;
8588
86#define GET_WORD(mem, index) ((mem)[(index)]|((mem)[(index)+1]<<8))
89/****************************************
8790
91   Graphics Drawing
92
93****************************************/
94
8895/*
89mode from blitter command
90#define SHADOW         (0x07)
91#define XORSHADOW      (0x06)
92#define NONCOLLIDABLE  (0x05)
93#define NORMAL         (0x04)
94#define BOUNDARY       (0x03)
95#define BOUNDARYSHADOW (0x02)
96#define BKGRNDNOCOL    (0x01)
97#define BKGRND         (0x00)
962008-10 FP:
97Current implementation: lynx_blitter reads what will be drawn and sets which line_functions to use.
98It then calls lynx_blit_lines which sets the various flip bits (horizontal and vertical) and calls
99the chosen line_function. These functions (available in various versions, depending on how many
100color bits are to be used) finally call lynx_plot_pixel which draws the sprite.
101
102Notice however that, based on the problems in Electrocop, Jimmy Connors Tennis and Switchblade II
103(among the others), it clearly seems that something is being lost in some of the passages. From
104my partial understanding while comparing the code with the manual, I would suspect the loops in
105the line_functions to be not completely correct.
106
107This whole part will be eventually moved to video/ once I'm satisfied (or I give up).
98108*/
99109
110#define GET_WORD(mem, index) ((mem)[(index)]|((mem)[(index)+1]<<8))
111
112/* modes from blitter command */
113enum {
114   BACKGROUND = 0,
115   BACKGROUND_NO_COLL,
116   BOUNDARY_SHADOW,
117   BOUNDARY,
118   NORMAL_SPRITE,
119   NO_COLL,
120   XOR_SPRITE,
121   SHADOW
122};
123
124static UINT8 sprite_collide;
125
126/* The pen numbers range from '0' to 'F. Pen numbers '1' thru 'D' are always collidable and opaque. The other
127ones have different behavior depending on the sprite type: there are 8 types of sprites, each has different
128characteristics relating to some or all of their pen numbers.
129
130* Shadow Error: The hardware is missing an inverter in the 'shadow' generator. This causes sprite types that
131did not invoke shadow to now invoke it and vice versa. The only actual functionality loss is that 'exclusive or'
132sprites and 'background' sprites will have shadow enabled.
133
134The sprite types relate to specific hardware functions according to the following table:
135
136
137   -------------------------- SHADOW
138  |   ----------------------- BOUNDARY_SHADOW
139  |  |   -------------------- NORMAL_SPRITE
140  |  |  |   ----------------- BOUNDARY
141  |  |  |  |   -------------- BACKGROUND (+ shadow, due to bug in 'E' pen)
142  |  |  |  |  |   ----------- BACKGROUND_NO_COLL
143  |  |  |  |  |  |   -------- NO_COLL
144  |  |  |  |  |  |  |   ----- XOR_SPRITE (+ shadow, due to bug in 'E' pen)
145  |  |  |  |  |  |  |  |
146  1  0  1  0  1  1  1  1      F is opaque
147  0  0  1  1  0  0  0  0      E is collideable
148  0  0  1  1  0  0  0  0      0 is opaque and collideable
149  1  1  1  1  0  0  0  1      allow collision detect
150  1  1  1  1  1  0  0  1      allow coll. buffer access
151  0  0  0  0  0  0  0  1      exclusive-or the data
152*/
153
100154INLINE void lynx_plot_pixel(const int mode, const int x, const int y, const int color)
101155{
102    int back;
103    UINT8 *screen;
104    UINT8 *colbuf;
156   int back;
157   UINT8 *screen;
158   UINT8 *colbuf;
105159
106    blitter.everon=TRUE;
107    screen=blitter.mem+blitter.screen+y*80+x/2;
108    colbuf=blitter.mem+blitter.colbuf+y*80+x/2;
109    switch (mode) {
110    case 0x00: // background (shadow bug!)
111   if (!(x&1)) {
112       *screen=(*screen&0x0f)|(color<<4);
113   } else {
114       *screen=(*screen&0xf0)|color;
160   blitter.everon = TRUE;
161   screen = blitter.mem + blitter.screen + y * 80 + x / 2;
162   colbuf = blitter.mem + blitter.colbuf + y * 80 + x / 2;
163
164   switch (mode)
165   {
166      case NORMAL_SPRITE:
167      /* A sprite may be set to 'normal'. This means that pen number '0' will be transparent and
168      non-collideable. All other pens will be opaque and collideable */
169         if (color == 0)
170            break;
171         if (!(x & 0x01))      /* Upper nibble */
172         {
173            *screen = (*screen & 0x0f) | (color << 4);
174
175            if(sprite_collide)
176            {
177               back = *colbuf;
178               *colbuf = (back & ~0xf0) | (blitter.spritenr << 4);
179               if ((back >> 4) > blitter.mem[blitter.colpos])
180                  blitter.mem[blitter.colpos] = back >> 4;
181               blitter.memory_accesses += 2;
182            }
183         }
184         else               /* Lower nibble */
185         {
186            *screen = (*screen & 0xf0) | color;
187
188            if(sprite_collide)
189            {
190               back = *colbuf;
191               *colbuf = (back & ~0x0f) | (blitter.spritenr);
192               if ((back & 0x0f) > blitter.mem[blitter.colpos])
193                  blitter.mem[blitter.colpos] = back & 0x0f;
194               blitter.memory_accesses += 2;
195            }
196         }
197         blitter.memory_accesses++;
198         break;
199
200      case BOUNDARY:
201      /* A sprite may be set to 'boundary'. This is a 'normal' sprite with the exception that pen
202      number 'F' is transparent (and still collideable). */
203         if (color == 0)
204            break;         
205         if (!(x & 0x01))      /* Upper nibble */
206         {
207            if (color != 0x0f)
208            {
209               *screen = (*screen & 0x0f) | (color << 4);
210               blitter.memory_accesses++;
211            }
212            if(sprite_collide)
213            {
214               back = *colbuf;
215               *colbuf = (back & ~0xf0) | (blitter.spritenr << 4);
216               if ((back >> 4) > blitter.mem[blitter.colpos])
217                  blitter.mem[blitter.colpos] = back >> 4;
218               blitter.memory_accesses += 2;
219            }
220         }
221         else               /* Lower nibble */
222         {
223            if (color != 0x0f)
224            {
225               *screen = (*screen & 0xf0) | color;
226               blitter.memory_accesses++;
227            }
228            if(sprite_collide)
229            {
230               back = *colbuf;
231               *colbuf = (back & ~0x0f) | (blitter.spritenr);
232               if ((back & 0x0f) > blitter.mem[blitter.colpos])
233                  blitter.mem[blitter.colpos] = back & 0x0f;
234               blitter.memory_accesses += 2;
235            }
236         }
237         break;
238
239      case SHADOW:
240      /* A sprite may be set to 'shadow'. This is a 'normal' sprite with the exception that pen
241      number 'E' is non-collideable (but still opaque) */
242         if (color == 0)
243            break;
244         if (!(x & 0x01))      /* Upper nibble */
245         {
246            *screen = (*screen & 0x0f) | (color << 4);
247
248            if (sprite_collide && (color != 0x0e))
249            {
250               back = *colbuf;
251               *colbuf = (back & ~0xf0) | (blitter.spritenr << 4);
252               if ((back >> 4) > blitter.mem[blitter.colpos])
253                  blitter.mem[blitter.colpos] = back >> 4;
254               blitter.memory_accesses += 2;
255            }
256         }
257         else               /* Lower nibble */
258         {
259            *screen = (*screen & 0xf0) | color;
260
261            if (sprite_collide && (color != 0x0e))
262            {
263               back = *colbuf;
264               *colbuf = (back & ~0x0f) | (blitter.spritenr);
265               if ((back & 0x0f) > blitter.mem[blitter.colpos])
266                  blitter.mem[blitter.colpos] = back & 0x0f;
267               blitter.memory_accesses += 2;
268            }
269         }
270         blitter.memory_accesses++;
271         break;
272
273      case BOUNDARY_SHADOW:
274      /* This sprite is a 'normal' sprite with the characteristics of both 'boundary'
275      and 'shadow'. That is, pen number 'F' is transparent (and still collideable) and
276      pen number 'E' is non-collideable (but still opaque). */
277         if (color == 0)
278            break;
279         if (!(x & 0x01))      /* Upper nibble */
280         {
281            if (color != 0x0f)
282            {
283               *screen = (*screen & 0x0f) | (color << 4);
284               blitter.memory_accesses++;
285            }
286            if (sprite_collide && (color != 0x0e))
287            {
288               back = *colbuf;
289               *colbuf = (back & ~0xf0) | (blitter.spritenr << 4);
290               if ((back >> 4) > blitter.mem[blitter.colpos])
291                  blitter.mem[blitter.colpos] = back >> 4;
292               blitter.memory_accesses += 2;
293            }
294         }
295         else               /* Lower nibble */
296         {
297            if (color != 0x0f)
298            {
299               *screen = (*screen & 0xf0) | color;
300               blitter.memory_accesses++;
301            }
302            if (sprite_collide && (color != 0x0e))
303            {
304               back = *colbuf;
305               *colbuf = (back & ~0x0f) | (blitter.spritenr);
306               if ((back & 0x0f) > blitter.mem[blitter.colpos])
307                  blitter.mem[blitter.colpos] = back & 0x0f;
308               blitter.memory_accesses += 2;
309            }
310         }
311         break;
312
313      case BACKGROUND:
314      /* A sprite may be set to 'background'. This sprite will overwrite the contents of the video and
315      collision buffers. Pens '0' and 'F' are no longer transparent. This sprite is used to initialize
316      the buffers at the start of a 'painting'. Additionally, no collision detection is done, and no write
317      to the collision depository occurs. The 'E' error will cause the pen number 'E' to be non-collideable
318      and therefore not clear the collision buffer */
319         if (!(x & 0x01))      /* Upper nibble */
320         {
321            *screen = (*screen & 0x0f) | (color << 4);
322
323            if (sprite_collide && (color != 0x0e))
324            {
325               *colbuf = (*colbuf & ~0xf0) | (blitter.spritenr << 4);
326               blitter.memory_accesses++;
327            }
328         }
329         else               /* Lower nibble */
330         {
331            *screen = (*screen & 0xf0) | color;
332
333            if (sprite_collide && (color != 0x0e))
334            {
335               *colbuf = (*colbuf & ~0x0f) | (blitter.spritenr);
336               blitter.memory_accesses++;
337            }
338         }
339         blitter.memory_accesses++;
340         break;
341
342      case BACKGROUND_NO_COLL:
343      /* This is a 'background' sprite with the exception that no activity occurs in the collision buffer */
344         if (!(x & 0x01))      /* Upper nibble */
345            *screen = (*screen & 0x0f) | (color << 4);
346         else               /* Lower nibble */
347            *screen = (*screen & 0xf0) | color;
348         blitter.memory_accesses++;
349         break;
350
351      case NO_COLL:
352      /* A sprite may be set to 'non-collideable'. This means that it will have no affect on the contents of
353      the collision buffer and all other collision activities are overridden (pen 'F' is not collideable). */
354         if (color == 0)
355            break;
356         if (!(x & 0x01))      /* Upper nibble */
357            *screen = (*screen & 0x0f) | (color << 4);
358         else               /* Lower nibble */
359            *screen = (*screen & 0xf0) | color;
360         blitter.memory_accesses++;
361         break;
362
363      case XOR_SPRITE:
364      /* This is a 'normal' sprite with the exception that the data from the video buffer is exclusive-ored
365      with the sprite data and written back out to the video buffer. Collision activity is 'normal'. The 'E'
366      error will cause the pen number 'E' to be non-collideable and therefore not react with the collision
367      buffer */
368         if (color == 0)
369            break;
370         if (!(x & 0x01))      /* Upper nibble */
371         {
372            *screen = (*screen & 0x0f)^(color<<4);
373
374            if (color != 0x0e)
375            {
376               back = *colbuf;
377               *colbuf = (back & ~0xf0) | (blitter.spritenr << 4);
378               if ((back >> 4) > blitter.mem[blitter.colpos])
379                  blitter.mem[blitter.colpos] = back >> 4;
380               blitter.memory_accesses += 2;
381            }
382         }
383         else               /* Lower nibble */
384         {
385            *screen = (*screen & 0xf0)^color;
386
387            if (color != 0x0e)
388            {
389               back = *colbuf;
390               *colbuf = (back & ~0x0f) | (blitter.spritenr);
391               if ((back & 0x0f) > blitter.mem[blitter.colpos])
392                  blitter.mem[blitter.colpos] = back & 0x0f;
393               blitter.memory_accesses += 2;
394            }
395         }
396         blitter.memory_accesses++;
397         break;
115398   }
116   blitter.memory_accesses++;
117   break;
118    case 0x10:
119   if (!(x&1)) {
120       if (color!=0xe) {
121      *colbuf=(*colbuf&~0xf0)|(blitter.spritenr<<4);
122      blitter.memory_accesses++;
123       }
124       *screen=(*screen&0x0f)|(color<<4);
125   } else {
126       if (color!=0xe) {
127      *colbuf=(*colbuf&~0xf)|(blitter.spritenr);
128      blitter.memory_accesses++;
129       }
130       *screen=(*screen&0xf0)|color;
399}
400
401static void lynx_blit_do_work( const int y, const int xdir, const int bits, const int mask )
402{
403   int j, xi, wi, i;
404   int b, p, color;
405
406   i = blitter.mem[blitter.bitmap];
407   blitter.memory_accesses++;      // ?
408   
409   for (xi = blitter.x, p = 0, b = 0, j = 1, wi = 0; j < i;)
410   {
411      if (p < bits)
412      {
413         b = (b << 8) | blitter.mem[blitter.bitmap + j];
414         j++;
415         p += 8;
416         blitter.memory_accesses++;
417      }
418      for ( ; (p >= bits); )
419      {
420         color = blitter.color[(b >> (p - bits)) & mask];
421         p -= bits;
422         for ( ; wi < blitter.width; wi += 0x100, xi += xdir)
423         {
424            if ((xi >= 0) && (xi < 160))
425               lynx_plot_pixel(blitter.mode, xi, y, color);
426         }
427         wi -= blitter.width;
428      }
131429   }
132   blitter.memory_accesses++;
133   break;
134    case 0x01: // background, no colliding
135    case 0x11:
136   if (!(x&1)) {
137       *screen=(*screen&0x0f)|(color<<4);
138   } else {
139       *screen=(*screen&0xf0)|color;
140   }
141   blitter.memory_accesses++;
142   break;
143    case 0x03: // boundary: pen P? transparent, but collides
144    case 0x02: // boundary, shadow
145   if ((color==0)||(color==0xf)) break;
146   if (!(x&1)) {
147       *screen=(*screen&0x0f)|(color<<4);
148   } else {
149       *screen=(*screen&0xf0)|color;
150   }
151   blitter.memory_accesses++;
152   break;
153    case 0x13:
154   if (color==0) break;
155   if (!(x&1)) {
156       back=*colbuf;
157       if (back>>4>blitter.mem[blitter.colpos])
158      blitter.mem[blitter.colpos]=back>>4;
159       *colbuf=(back&~0xf0)|(blitter.spritenr<<4);
160       if (color!=0xf) {
161      *screen=(*screen&0x0f)|(color<<4);
162      blitter.memory_accesses++;
163       }
164   } else {
165       back=*colbuf;
166       if ((back&0xf)>blitter.mem[blitter.colpos])
167      blitter.mem[blitter.colpos]=back&0xf;
168       *colbuf=(back&~0xf)|(blitter.spritenr);
169       if (color!=0xf) {
170      *screen=(*screen&0xf0)|color;
171      blitter.memory_accesses++;
172       }
173   }
174   blitter.memory_accesses+=2;
175   break;
176    case 0x12:
177   if (color==0) break;
178   if (!(x&1)) {
179       if (color!=0xe) {
180      back=*colbuf;
181      if (back>>4>blitter.mem[blitter.colpos])
182          blitter.mem[blitter.colpos]=back>>4;
183      *colbuf=(back&~0xf0)|(blitter.spritenr<<4);
184       }
185       if (color!=0xf) {
186      *screen=(*screen&0x0f)|(color<<4);
187      blitter.memory_accesses++;
188       }
189   } else {
190       if (color!=0xe) {
191      back=*colbuf;
192      if ((back&0xf)>blitter.mem[blitter.colpos])
193          blitter.mem[blitter.colpos]=back&0xf;
194      *colbuf=(back&~0xf)|(blitter.spritenr);
195       }
196       if (color!=0xf) {
197      *screen=(*screen&0xf0)|color;
198      blitter.memory_accesses++;
199       }
200   }
201   blitter.memory_accesses+=2;
202   break;
203    case 0x04: // pen 0 transparent,
204    case 0x07: // shadow: pen e doesn't collide
205    case 0x05: // non collidable sprite
206    case 0x15:
207   if (color==0) break;
208   if (!(x&1)) {
209       *screen=(*screen&0x0f)|(color<<4);
210   } else {
211       *screen=(*screen&0xf0)|color;
212   }
213   blitter.memory_accesses++;
214   break;
215    case 0x14:
216   if (color==0) break;
217   if (!(x&1)) {
218       back=*colbuf;
219       *colbuf=(back&~0xf0)|(blitter.spritenr<<4);
220       if (back>>4>blitter.mem[blitter.colpos])
221      blitter.mem[blitter.colpos]=back>>4;
222       *screen=(*screen&0x0f)|(color<<4);
223   } else {
224       back=*colbuf;
225       *colbuf=(back&~0xf)|(blitter.spritenr);
226       if ((back&0xf)>blitter.mem[blitter.colpos])
227      blitter.mem[blitter.colpos]=back&0xf;
228       *screen=(*screen&0xf0)|color;
229   }
230   blitter.memory_accesses+=3;
231   break;
232    case 0x17:
233   if (color==0) break;
234   if (!(x&1)) {
235       if (color!=0xe) {
236      back=*colbuf;
237      if (back>>4>blitter.mem[blitter.colpos])
238          blitter.mem[blitter.colpos]=back>>4;
239      *colbuf=(back&~0xf0)|(blitter.spritenr<<4);
240      blitter.memory_accesses+=2;
241       }
242       *screen=(*screen&0x0f)|(color<<4);
243   } else {
244       if (color!=0xe) {
245      back=*colbuf;
246      if ((back&0xf)>blitter.mem[blitter.colpos])
247          blitter.mem[blitter.colpos]=back&0xf;
248      *colbuf=(back&~0xf)|(blitter.spritenr);
249      blitter.memory_accesses+=2;
250       }
251       *screen=(*screen&0xf0)|color;
252   }
253   blitter.memory_accesses++;
254   break;
255    case 0x06: // xor sprite (shadow bug!)
256   if (!(x&1)) {
257       *screen=(*screen&0x0f)^(color<<4);
258   } else {
259       *screen=(*screen&0xf0)^color;
260   }
261   blitter.memory_accesses++;
262   break;
263    case 0x16:
264   if (!(x&1)) {
265       if (color!=0xe) {
266      back=*colbuf;
267      if (back>>4>blitter.mem[blitter.colpos])
268          blitter.mem[blitter.colpos]=back>>4;
269      *colbuf=(back&~0xf0)|(blitter.spritenr<<4);
270      blitter.memory_accesses+=2;
271       }
272       *screen=(*screen&0x0f)^(color<<4);
273   } else {
274       if (color!=0xe) {
275      back=*colbuf;
276      if ((back&0xf)>blitter.mem[blitter.colpos])
277          blitter.mem[blitter.colpos]=back&0xf;
278      *colbuf=(back&~0xf)|(blitter.spritenr);
279      blitter.memory_accesses+=2;
280       }
281       *screen=(*screen&0xf0)^color;
282   }
283   blitter.memory_accesses++;
284   break;
285    }
286430}
287431
288#define INCLUDE_LYNX_LINE_FUNCTION
432
289433static void lynx_blit_2color_line(const int y, const int xdir)
290434{
291   const int bits=1;
292   const int mask=0x1;
293#include "includes/lynx.h"
435   const int bits = 1;
436   const int mask = 0x01;
437
438   lynx_blit_do_work(y, xdir, bits, mask);
294439}
295440static void lynx_blit_4color_line(const int y, const int xdir)
296441{
297   const int bits=2;
298   const int mask=0x3;
299#include "includes/lynx.h"
442   const int bits = 2;
443   const int mask = 0x03;
444
445   lynx_blit_do_work(y, xdir, bits, mask);
300446}
301447static void lynx_blit_8color_line(const int y, const int xdir)
302448{
303   const int bits=3;
304   const int mask=0x7;
305#include "includes/lynx.h"
449   const int bits = 3;
450   const int mask = 0x07;
451
452   lynx_blit_do_work(y, xdir, bits, mask);
306453}
307454static void lynx_blit_16color_line(const int y, const int xdir)
308455{
309   const int bits=4;
310   const int mask=0xf;
311#include "includes/lynx.h"
456   const int bits = 4;
457   const int mask = 0x0f;
458
459   lynx_blit_do_work(y, xdir, bits, mask);
312460}
313#undef INCLUDE_LYNX_LINE_FUNCTION
314461
315462/*
3164632 color rle: ??
r3406r3407
335482 0, 4 bit repeat count-1, 4 bit color
336483 1, 4 bit count of values-1, 4 bit color, ....
337484*/
338#define INCLUDE_LYNX_LINE_RLE_FUNCTION
485
486static void lynx_blit_rle_do_work( const int y, const int xdir, const int bits, const int mask )
487{
488   int wi, xi;
489   int b, p, j;
490   int t, count, color;
491
492   for( p = 0, j = 0, b = 0, xi = blitter.x, wi = 0; ; )      /* through the rle entries */
493   {
494      if (p < 5 + bits) /* under 7 bits no complete entry */
495      {
496         j++;
497         if (j >= blitter.mem[blitter.bitmap])
498            return;
499
500         p += 8;
501         b = (b << 8) | blitter.mem[blitter.bitmap + j];
502         blitter.memory_accesses++;
503      }
504
505      t = (b >> (p - 1)) & 0x01;
506      p--;
507      count = ((b >> (p - 4)) & 0x0f) + 1;
508      p -= 4;
509
510      if (t)      /* count of different pixels */
511      {
512         for ( ; count; count--)
513         {
514            if (p < bits)
515            {
516               j++;
517               if (j >= blitter.mem[blitter.bitmap])
518                  return;
519               p += 8;
520               b = (b << 8) | blitter.mem[blitter.bitmap + j];
521               blitter.memory_accesses++;
522            }
523
524            color = blitter.color[(b >> (p - bits)) & mask];
525            p -= bits;
526            for ( ; wi < blitter.width; wi += 0x100, xi += xdir)
527            {
528               if ((xi >= 0) && (xi < 160))
529                  lynx_plot_pixel(blitter.mode, xi, y, color);
530            }
531            wi -= blitter.width;
532         }
533      }
534      else      /* count of same pixels */
535      {
536         if (count == 0)
537            return;
538
539         if (p < bits)
540         {
541            j++;
542            if (j >= blitter.mem[blitter.bitmap])
543               return;
544            p += 8;
545            b = (b << 8) | blitter.mem[blitter.bitmap + j];
546            blitter.memory_accesses++;
547         }
548
549         color = blitter.color[(b >> (p - bits)) & mask];
550         p -= bits;
551
552         for ( ; count; count--)
553         {
554            for ( ; wi < blitter.width; wi += 0x100, xi += xdir)
555            {
556               if ((xi >= 0) && (xi < 160))
557                  lynx_plot_pixel(blitter.mode, xi, y, color);
558            }
559            wi -= blitter.width;
560         }
561      }
562   }
563}
564
339565static void lynx_blit_2color_rle_line(const int y, const int xdir)
340566{
341   const int bits=1;
342   const int mask=0x1;
343#include "includes/lynx.h"
567   const int bits = 1;
568   const int mask = 0x01;
569
570   lynx_blit_rle_do_work(y, xdir, bits, mask);
344571}
345572static void lynx_blit_4color_rle_line(const int y, const int xdir)
346573{
347   const int bits=2;
348   const int mask=0x3;
349#include "includes/lynx.h"
574   const int bits = 2;
575   const int mask = 0x03;
576
577   lynx_blit_rle_do_work(y, xdir, bits, mask);
350578}
351579static void lynx_blit_8color_rle_line(const int y, const int xdir)
352580{
353   const int bits=3;
354   const int mask=0x7;
355#include "includes/lynx.h"
581   const int bits = 3;
582   const int mask = 0x07;
583
584   lynx_blit_rle_do_work(y, xdir, bits, mask);
356585}
357586static void lynx_blit_16color_rle_line(const int y, const int xdir)
358587{
359   const int bits=4;
360   const int mask=0xf;
361#include "includes/lynx.h"
588   const int bits = 4;
589   const int mask = 0x0f;
590
591   lynx_blit_rle_do_work(y, xdir, bits, mask);
362592}
363#undef INCLUDE_LYNX_LINE_RLE_FUNCTION
364593
365594
366595static void lynx_blit_lines(void)
367596{
368597   int i, hi, y;
369   int ydir=0, xdir=0;
370   int flip=blitter.mem[blitter.cmd+1]&3;
598   int ydir = 0, xdir = 0;
599   int flip;
371600
372   blitter.everon=FALSE;
601   blitter.everon = FALSE;
373602
374603   // flipping sprdemo3
375604   // fat bobby 0x10
376605   // mirror the sprite in gameplay?
377   xdir=1;
378   if (blitter.mem[blitter.cmd]&0x20) { xdir=-1;blitter.x--;/*?*/ }
379   ydir=1;
380   if (blitter.mem[blitter.cmd]&0x10) { ydir=-1;blitter.y--;/*?*/ }
381   switch (blitter.mem[blitter.cmd+1]&3) {
382   case 0:
383      flip =0;
384      break;
385   case 1: // blockout
386      xdir*=-1;
387      flip=1;
388      break;
389   case 2: // fat bobby horicontal
390      ydir*=-1;
391      flip=1;
392      break;
393   case 3:
394      xdir*=-1;
395      ydir*=-1;
396      flip=3;
397      break;
606   xdir = 1;
607
608   if (blitter.mem[blitter.cmd] & 0x20)   /* Horizontal Flip */
609   {
610      xdir = -1;
611      blitter.x--;   /*?*/
398612   }
613   
614   ydir = 1;
399615
400   for ( y=blitter.y, hi=0; (blitter.memory_accesses++,i=blitter.mem[blitter.bitmap]); blitter.bitmap+=i ) {
401      if (i==1) {
402          // centered sprites sprdemo3, fat bobby, blockout
403          hi=0;
404          switch (flip&3) {
405          case 0:
406          case 2:
407         ydir*=-1;
408         blitter.y+=ydir;
616   if (blitter.mem[blitter.cmd] & 0x10)    /* Vertical Flip */
617   {
618      ydir = -1;
619      blitter.y--;   /*?*/
620   }
621   
622   switch (blitter.mem[blitter.cmd + 1] & 0x03)   /* Start Left & Start Up */
623   {
624      case 0:
625         flip =0;
409626         break;
410          case 1:
411          case 3:
412         xdir*=-1;
413         blitter.x+=xdir;
627
628      case 1: // blockout
629         xdir *= -1;
630         flip = 1;
414631         break;
415          }
416          y=blitter.y;
417          flip++;
632
633      case 2: // fat bobby horicontal
634         ydir *= -1;
635         flip = 1;
636         break;
637
638      case 3:
639         xdir *= -1;
640         ydir *= -1;
641         flip = 3;
642         break;
643   }
644
645   for (y = blitter.y, hi = 0; blitter.memory_accesses++, i = blitter.mem[blitter.bitmap]; blitter.bitmap += i )
646   {
647      if (i == 1)
648      {
649         // centered sprites sprdemo3, fat bobby, blockout
650         hi = 0;
651         switch (flip & 0x03)
652         {
653            case 0:
654            case 2:
655               ydir *= -1;
656               blitter.y += ydir;
657               break;
658
659            case 1:
660            case 3:
661               xdir *= -1;
662               blitter.x += xdir;
663               break;
664         }
665
666         y = blitter.y;
667         flip++;
418668          continue;
419669      }
420      for (;(hi<blitter.height); hi+=0x100, y+=ydir) {
421          if ( y>=0 && y<102 )
422         blitter.line_function(y,xdir);
423          blitter.width+=blitter.stretch;
424          blitter.x+=blitter.tilt;
670
671      for ( ; (hi < blitter.height); hi += 0x100, y += ydir)
672      {
673         if (y >= 0 && y < 102)
674            blitter.line_function(y, xdir);
675         blitter.width += blitter.stretch;
676         blitter.x += blitter.tilt;
425677      }
426      hi-=blitter.height;
678
679      hi -= blitter.height;
427680   }
428   switch (blitter.mode) {
429   case 0x12: case 0x13: case 0x14: case 0x16: case 0x17:
430       if (suzy.u.s.SPRG0&0x20 && blitter.everon) {
431      blitter.mem[blitter.colpos]|=0x80;
432       }
681
682   if (suzy.data[SPRG0] & 0x04)
683   {
684      if (sprite_collide & !blitter.everon)
685         blitter.mem[blitter.colpos] |= 0x80;
686      else
687         blitter.mem[blitter.colpos] &= 0x7f;
433688   }
434689}
435690
436691static TIMER_CALLBACK(lynx_blitter_timer)
437692{
438    suzy.u.s.SPRSYS&=~1; //blitter finished
693   suzy.data[SPRSYS] &= ~0x01; //blitter finished
439694}
440695
441696/*
r3406r3407
513768
514769static void lynx_blitter(void)
515770{
516    static const int lynx_colors[4]={2,4,8,16};
771   static const int lynx_colors[4]={2,4,8,16};
517772
518    static void (* const blit_line[4])(const int y, const int xdir)= {
773   static void (* const blit_line[4])(const int y, const int xdir)= {
519774   lynx_blit_2color_line,
520775   lynx_blit_4color_line,
521776   lynx_blit_8color_line,
522777   lynx_blit_16color_line
523    };
778   };
524779
525    static void (* const blit_rle_line[4])(const int y, const int xdir)= {
780   static void (* const blit_rle_line[4])(const int y, const int xdir)= {
526781   lynx_blit_2color_rle_line,
527782   lynx_blit_4color_rle_line,
528783   lynx_blit_8color_rle_line,
529784   lynx_blit_16color_rle_line
530    };
531    int i; int o;int colors;
785   };
786   int i; int o;int colors;
532787
533    blitter.memory_accesses=0;
534    blitter.mem = memory_get_read_ptr(0, ADDRESS_SPACE_PROGRAM, 0x0000);
535    blitter.colbuf=GET_WORD(suzy.u.data, 0xa);
536    blitter.screen=GET_WORD(suzy.u.data, 8);
537    blitter.xoff=GET_WORD(suzy.u.data,4);
538    blitter.yoff=GET_WORD(suzy.u.data,6);
539    // hsizeoff GET_WORD(suzy.u.data, 0x28)
540    // vsizeoff GET_WORD(suzy.u.data, 0x2a)
788   blitter.memory_accesses = 0;
789   blitter.mem = memory_get_read_ptr(0, ADDRESS_SPACE_PROGRAM, 0x0000);
541790
542    // these might be never set by the blitter hardware
543    blitter.width=0x100;
544    blitter.height=0x100;
545    blitter.stretch=0;
546    blitter.tilt=0;
791   blitter.xoff   = GET_WORD(suzy.data, 0x04);
792   blitter.yoff   = GET_WORD(suzy.data, 0x06);
793   blitter.screen = GET_WORD(suzy.data, 0x08);
794   blitter.colbuf = GET_WORD(suzy.data, 0x0a);
795   // hsizeoff GET_WORD(suzy.data, 0x28)
796   // vsizeoff GET_WORD(suzy.data, 0x2a)
547797
548    blitter.memory_accesses+=2;
549    for (blitter.cmd=GET_WORD(suzy.u.data, 0x10); blitter.cmd; ) {
798   // these might be never set by the blitter hardware
799   blitter.width   = 0x100;
800   blitter.height  = 0x100;
801   blitter.stretch = 0;
802   blitter.tilt    = 0;
803   sprite_collide  = 0;
550804
551   blitter.memory_accesses+=1;
552   if (!(blitter.mem[blitter.cmd+1]&4)) {
805   blitter.memory_accesses += 2;
553806
554       blitter.colpos=GET_WORD(suzy.u.data, 0x24)+blitter.cmd;
807   for (blitter.cmd = GET_WORD(suzy.data, 0x10); blitter.cmd; )
808   {
809      blitter.memory_accesses += 1;
555810
556       blitter.bitmap=GET_WORD(blitter.mem,blitter.cmd+5);
557       blitter.x=(INT16)GET_WORD(blitter.mem, blitter.cmd+7)-blitter.xoff;
558       blitter.y=(INT16)GET_WORD(blitter.mem, blitter.cmd+9)-blitter.yoff;
559       blitter.memory_accesses+=6;
811      if (!(blitter.mem[blitter.cmd + 1] & 0x04))      // if 0, we skip this sprite
812      {
813         blitter.colpos = GET_WORD(suzy.data, 0x24) + blitter.cmd;
560814
561       blitter.mode=blitter.mem[blitter.cmd]&07;
562       if (blitter.mem[blitter.cmd+1]&0x80) {
563      blitter.line_function=blit_line[blitter.mem[blitter.cmd]>>6];
564       } else {
565      blitter.line_function=blit_rle_line[blitter.mem[blitter.cmd]>>6];
566       }
815         blitter.bitmap = GET_WORD(blitter.mem,blitter.cmd + 5);
816         blitter.x = (INT16)GET_WORD(blitter.mem, blitter.cmd + 7) - blitter.xoff;
817         blitter.y = (INT16)GET_WORD(blitter.mem, blitter.cmd + 9) - blitter.yoff;
818         blitter.memory_accesses += 6;
567819
568       if (!(blitter.mem[blitter.cmd+2]&0x20) && !( suzy.u.s.SPRSYS&0x20) ) {
569      switch (blitter.mode) {
570      case 0: case 2: case 3: case 4: case 6: case 7:
571          blitter.mode|=0x10;
572          blitter.mem[blitter.colpos]=0;
573          blitter.spritenr=blitter.mem[blitter.cmd+2]&0xf;
574      }
575       }
820         blitter.mode = blitter.mem[blitter.cmd] & 0x07;
576821
577       o=0xb;
578       if (blitter.mem[blitter.cmd+1]&0x30) {
579      blitter.width=GET_WORD(blitter.mem, blitter.cmd+11);
580      blitter.height=GET_WORD(blitter.mem, blitter.cmd+13);
581      blitter.memory_accesses+=4;
582      o+=4;
583       }
822         if (blitter.mem[blitter.cmd + 1] & 0x80)
823            blitter.line_function = blit_line[blitter.mem[blitter.cmd] >> 6];
824         else
825            blitter.line_function = blit_rle_line[blitter.mem[blitter.cmd] >> 6];
584826
585       if (blitter.mem[blitter.cmd+1]&0x20) {
586      blitter.stretch=GET_WORD(blitter.mem, blitter.cmd+o);
587      blitter.memory_accesses+=2;
588      o+=2;
589      if (blitter.mem[blitter.cmd+1]&0x10) {
590          blitter.tilt=GET_WORD(blitter.mem, blitter.cmd+o);
591          blitter.memory_accesses+=2;
592          o+=2;
827         if (!(blitter.mem[blitter.cmd + 2] & 0x20) && !(suzy.data[SPRSYS] & 0x20))
828         {
829            switch (blitter.mode)
830            {
831               case BACKGROUND:
832               case BOUNDARY_SHADOW:
833               case BOUNDARY:
834               case NORMAL_SPRITE:
835               case XOR_SPRITE:
836               case SHADOW:
837                  sprite_collide = 1;
838                  blitter.mem[blitter.colpos] = 0;
839                  blitter.spritenr = blitter.mem[blitter.cmd + 2] & 0x0f;
840            }
841         }
842
843         /* Sprite Reload Bits */
844         o = 0x0b;
845         if (blitter.mem[blitter.cmd + 1] & 0x30)
846         {
847            blitter.width  = GET_WORD(blitter.mem, blitter.cmd + 11);
848            blitter.height = GET_WORD(blitter.mem, blitter.cmd + 13);
849            blitter.memory_accesses += 4;
850            o += 4;
851         }
852         if (blitter.mem[blitter.cmd + 1] & 0x20)
853         {
854            blitter.stretch = GET_WORD(blitter.mem, blitter.cmd + o);
855            blitter.memory_accesses += 2;
856            o += 2;
857            if (blitter.mem[blitter.cmd + 1] & 0x10)
858            {
859               blitter.tilt = GET_WORD(blitter.mem, blitter.cmd+o);
860               blitter.memory_accesses += 2;
861               o += 2;
862            }
863         }
864
865         /* Reload Palette Bit */
866         colors = lynx_colors[blitter.mem[blitter.cmd] >> 6];
867
868         if (!(blitter.mem[blitter.cmd + 1] & 0x08))
869         {
870            for (i = 0; i < colors / 2; i++)
871            {
872               blitter.color[i * 2]      = blitter.mem[blitter.cmd + o + i] >> 4;
873               blitter.color[i * 2 + 1 ] = blitter.mem[blitter.cmd + o + i] & 0x0f;
874               blitter.memory_accesses++;
875            }
876         }
877         
878         /* Draw Sprites */
879         lynx_blit_lines();
593880      }
594       }
595       colors=lynx_colors[blitter.mem[blitter.cmd]>>6];
596881
597       if (!(blitter.mem[blitter.cmd+1]&8)) {
598      for (i=0; i<colors/2; i++) {
599          blitter.color[i*2]=blitter.mem[blitter.cmd+o+i]>>4;
600          blitter.color[i*2+1]=blitter.mem[blitter.cmd+o+i]&0xf;
601          blitter.memory_accesses++;
602      }
603       }
604       lynx_blit_lines();
882   blitter.cmd = GET_WORD(blitter.mem,  blitter.cmd + 3);
883   blitter.memory_accesses += 2;
884
885   if (!(blitter.cmd & 0xff00))
886      break;
605887   }
606   blitter.cmd=GET_WORD(blitter.mem, blitter.cmd+3);
607   blitter.memory_accesses+=2;
608   if (!(blitter.cmd&0xff00)) break;
609    }
610888
611889   if (0)
612890      timer_set(ATTOTIME_IN_CYCLES(blitter.memory_accesses*20,0), NULL, 0, lynx_blitter_timer);
613891}
614892
615static void lynx_divide(void)
893
894/****************************************
895
896   Suzy Emulation
897
898****************************************/
899
900
901/* Math bugs of the original hardware:
902
903- in signed multiply, the hardware thinks that 8000 is a positive number
904- in signed multiply, the hardware thinks that 0 is a negative number. This is not an immediate
905problem for a multiply by zero, since the answer will be re-negated to the correct polarity of
906zero. However, since it will set the sign flag, you can not depend on the sign flag to be correct
907if you just load the lower byte after a multiply by zero.
908- in divide, the remainder will have 2 possible errors, depending on its actual value (no further
909notes on these errors available) */
910
911static void lynx_divide( void )
616912{
617   UINT32 left=suzy.u.s.H|(suzy.u.s.G<<8)|(suzy.u.s.F<<16)|(suzy.u.s.E<<24);
618   UINT16 right=suzy.u.s.P|(suzy.u.s.N<<8);
913   UINT32 left;
914   UINT16 right;
619915   UINT32 res, mod;
620   suzy.accumulate_overflow=FALSE;
621   if (right==0) {
622       suzy.accumulate_overflow=TRUE;
623       res=0xffffffff;
624       mod=0; //?
625   } else {
626       res=left/right;
627       mod=left%right;
916   /*
917   Hardware divide:
918            EFGH
919   *           NP
920   ----------------
921            ABCD
922   Remainder (JK)LM
923   */
924
925   left = suzy.data[MATH_H] | (suzy.data[MATH_G] << 8) | (suzy.data[MATH_F] << 16) | (suzy.data[MATH_E] << 24);
926   right = suzy.data[MATH_P] | (suzy.data[MATH_N] << 8);
927
928   suzy.accumulate_overflow = FALSE;
929   if (right == 0)
930   {
931      suzy.accumulate_overflow = TRUE;   /* during divisions, this bit is used to detect denominator = 0 */
932      res = 0xffffffff;
933      mod = 0; //?
934   }
935   else
936   {
937      res = left / right;
938      mod = left % right;
628939   }
629940//   logerror("coprocessor %8x / %8x = %4x\n", left, right, res);
630   suzy.u.s.D=res&0xff;
631   suzy.u.s.C=res>>8;
632   suzy.u.s.B=res>>16;
633   suzy.u.s.A=res>>24;
634   suzy.u.s.M=mod&0xff;
635   suzy.u.s.L=mod>>8;
636   suzy.u.s.K=mod>>16;
637   suzy.u.s.J=mod>>24;
941   suzy.data[MATH_D] = res & 0xff;
942   suzy.data[MATH_C] = res >> 8;
943   suzy.data[MATH_B] = res >> 16;
944   suzy.data[MATH_A] = res >> 24;
945
946   suzy.data[MATH_M] = mod & 0xff;
947   suzy.data[MATH_L] = mod >> 8;
948   suzy.data[MATH_K] = mod >> 16;
949   suzy.data[MATH_J] = mod >> 24;
638950}
639951
640static void lynx_multiply(void)
952static void lynx_multiply( void )
641953{
642954   UINT16 left, right;
643955   UINT32 res, accu;
644   left=suzy.u.s.B|(suzy.u.s.A<<8);
645   right=suzy.u.s.D|(suzy.u.s.C<<8);
646   if (suzy.u.s.SPRSYS&0x80) {
647       // to do
648       res=(INT16)left*(INT16)right;
649   } else res=left*right;
650//   logerror("coprocessor %4x * %4x = %4x\n", left, right, res);
651   suzy.u.s.H=res&0xff;
652   suzy.u.s.G=res>>8;
653   suzy.u.s.F=res>>16;
654   suzy.u.s.E=res>>24;
655   if (suzy.u.s.SPRSYS&0x40) {
656       accu=suzy.u.s.M|suzy.u.s.L<<8|suzy.u.s.K<<16|suzy.u.s.J<<24;
657       accu+=res;
658       if (accu<res) suzy.accumulate_overflow=TRUE;
659       suzy.u.s.M=accu;
660       suzy.u.s.L=accu>>8;
661       suzy.u.s.K=accu>>16;
662       suzy.u.s.J=accu>>24;
956   /*
957   Hardware multiply:
958              AB
959   *           CD
960   ----------------
961            EFGH
962   Accumulate   JKLM
963   */
964   suzy.accumulate_overflow = FALSE;
965
966   left = suzy.data[MATH_B] | (suzy.data[MATH_A] << 8);
967   right = suzy.data[MATH_D] | (suzy.data[MATH_C] << 8);
968
969   res = left * right;
970
971   if (suzy.data[SPRSYS] & 0x80)      /* signed math */
972   {
973      if (!(sign_AB + sign_CD))   /* different signs */
974         res = (res ^ 0xffffffff) + 1;
975   }
976
977   suzy.data[MATH_H] = res & 0xff;
978   suzy.data[MATH_G] = res >> 8;
979   suzy.data[MATH_F] = res >> 16;
980   suzy.data[MATH_E] = res >> 24;
981
982   if (suzy.data[SPRSYS] & 0x40)      /* is accumulation allowed? */
983   {
984      accu = suzy.data[MATH_M] | suzy.data[MATH_L] << 8 | suzy.data[MATH_K] << 16 | suzy.data[MATH_J] << 24;
985      accu += res;
986
987      if (accu < res)
988         suzy.accumulate_overflow = TRUE;
989
990      suzy.data[MATH_M] = accu;
991      suzy.data[MATH_L] = accu >> 8;
992      suzy.data[MATH_K] = accu >> 16;
993      suzy.data[MATH_J] = accu >> 24;
663994   }
664995}
665996
666 READ8_HANDLER(suzy_read)
997READ8_HANDLER( suzy_read )
667998{
668   UINT8 data=0, input;
669   switch (offset) {
670   case 0x88:
671      data=1; // must not be 0 for correct power up
672      break;
673   case 0x92:
674      if (!attotime_compare(blitter.time, attotime_zero))
675      {
676         if ( ATTOTIME_TO_CYCLES(0,attotime_sub(timer_get_time(), blitter.time)) > blitter.memory_accesses*20)
999   UINT8 value = 0, input;
1000
1001   switch (offset)
1002   {
1003      case 0x88:
1004         value = 0x01; // must not be 0 for correct power up
1005         break;
1006      case 0x92:   /* Better check this with docs! */
1007         if (!attotime_compare(blitter.time, attotime_zero))
6771008         {
678            suzy.u.data[offset]&=~1; //blitter finished
679            blitter.time = attotime_zero;
1009            if (ATTOTIME_TO_CYCLES(0, attotime_sub(timer_get_time(), blitter.time)) > blitter.memory_accesses * 20)
1010            {
1011               suzy.data[offset] &= ~0x01; //blitter finished
1012               blitter.time = attotime_zero;
1013            }
6801014         }
681      }
682      data=suzy.u.data[offset];
683      data&=~0x80; // math finished
684      data&=~0x40;
685      if (suzy.accumulate_overflow) data|=0x40;
686      break;
687   case 0xb0:
688      input=input_port_read(machine, "JOY");
689      switch (lynx_rotate) {
690      case 1:
691         data=input;
692         input&=0xf;
693         if (data&PAD_UP) input|=PAD_LEFT;
694         if (data&PAD_LEFT) input|=PAD_DOWN;
695         if (data&PAD_DOWN) input|=PAD_RIGHT;
696         if (data&PAD_RIGHT) input|=PAD_UP;
1015         value = suzy.data[offset];
1016         value &= ~0x80; // math finished
1017         value &= ~0x40;
1018         if (suzy.accumulate_overflow)
1019            value |= 0x40;
6971020         break;
698      case 2:
699         data=input;
700         input&=0xf;
701         if (data&PAD_UP) input|=PAD_RIGHT;
702         if (data&PAD_RIGHT) input|=PAD_DOWN;
703         if (data&PAD_DOWN) input|=PAD_LEFT;
704         if (data&PAD_LEFT) input|=PAD_UP;
1021      case 0xb0:
1022         input = input_port_read(machine, "JOY");
1023         switch (lynx_rotate)
1024         {
1025            case 1:
1026               value = input;
1027               input &= 0x0f;
1028               if (value & PAD_UP) input |= PAD_LEFT;
1029               if (value & PAD_LEFT) input |= PAD_DOWN;
1030               if (value & PAD_DOWN) input |= PAD_RIGHT;
1031               if (value & PAD_RIGHT) input |= PAD_UP;
1032               break;
1033            case 2:
1034               value = input;
1035               input &= 0x0f;
1036               if (value & PAD_UP) input |= PAD_RIGHT;
1037               if (value & PAD_RIGHT) input |= PAD_DOWN;
1038               if (value & PAD_DOWN) input |= PAD_LEFT;
1039               if (value & PAD_LEFT) input |= PAD_UP;
1040               break;
1041         }
1042         if (suzy.data[SPRSYS] & 0x08) /* Left handed controls */
1043         {
1044            value = input & 0x0f;
1045            if (input & PAD_UP) value |= PAD_DOWN;
1046            if (input & PAD_DOWN) value |= PAD_UP;
1047            if (input & PAD_LEFT) value |= PAD_RIGHT;
1048            if (input & PAD_RIGHT) value |= PAD_LEFT;
1049         }
1050         else
1051            value = input;
7051052         break;
706      }
707      if (suzy.u.s.SPRSYS&8) {
708         data=input&0xf;
709         if (input&PAD_UP) data|=PAD_DOWN;
710         if (input&PAD_DOWN) data|=PAD_UP;
711         if (input&PAD_LEFT) data|=PAD_RIGHT;
712         if (input&PAD_RIGHT) data|=PAD_LEFT;
713      } else {
714         data=input;
715      }
716      break;
717   case 0xb1: data=input_port_read(machine, "PAUSE");break;
718   case 0xb2:
719      data=*(memory_region(machine, "user1")+(suzy.high*lynx_granularity)+suzy.low);
720      suzy.low=(suzy.low+1)&(lynx_granularity-1);
721      break;
722   default:
723      data=suzy.u.data[offset];
1053      case 0xb1:
1054         value = input_port_read(machine, "PAUSE");
1055         break;
1056      case 0xb2:
1057         value = *(memory_region(machine, "user1") + (suzy.high * lynx_granularity) + suzy.low);
1058         suzy.low = (suzy.low + 1) & (lynx_granularity - 1);
1059         break;
1060      case 0xb3: /* we need bank 1 emulation!!! */
1061      default:
1062         value = suzy.data[offset];
7241063   }
7251064//   logerror("suzy read %.2x %.2x\n",offset,data);
726   return data;
1065   return value;
7271066}
7281067
7291068WRITE8_HANDLER(suzy_write)
7301069{
731   suzy.u.data[offset]=data;
732   switch(offset) {
733   case 0x52: case 0x54: case 0x56:
734   case 0x60: case 0x62:
735   case 0x6e:
736       suzy.u.data[offset+1]=0;
737       break;
1070   suzy.data[offset] = data;
1071
1072   /* Additional effects of a write */
1073   /* Even addresses are the LSB. Any CPU write to an LSB in 0x00-0x7f will set the MSB to 0. */
1074   /* This in particular holds for math quantities:  Writing to B (0x54), D (0x52),
1075   F (0x62), H (0x60), K (0x6e) or M (0x6c) will force a '0' to be written to A (0x55),
1076   C (0x53), E (0x63), G (0x61), J (0x6f) or L (0x6d) respectively */
1077   switch(offset)
1078   {
1079   case 0x00: case 0x02: case 0x04: case 0x06: case 0x08: case 0x0a: case 0x0c: case 0x0e:
1080   case 0x10: case 0x12: case 0x14: case 0x16: case 0x18: case 0x1a: case 0x1c: case 0x1e:
1081   case 0x20: case 0x22: case 0x24: case 0x26: case 0x28: case 0x2a: case 0x2c: case 0x2e:
1082   case 0x30: case 0x32: case 0x34: case 0x36: case 0x38: case 0x3a: case 0x3c: case 0x3e:
1083   case 0x40: case 0x42: case 0x44: case 0x46: case 0x48: case 0x4a: case 0x4c: case 0x4e:
1084   case 0x50: case 0x56: case 0x58: case 0x5a: case 0x5c: case 0x5e:
1085   case 0x64: case 0x66: case 0x68: case 0x6a:
1086   case 0x70: case 0x72: case 0x74: case 0x76: case 0x78: case 0x7a: case 0x7c: case 0x7e:
1087   /* B, D, F, H , K */
1088   case 0x52: case 0x54: case 0x60: case 0x62: case 0x6e:
1089      suzy.data[offset + 1] = 0;
1090      break;
1091   /* Writing to M (0x6c) will also clear the accumulator overflow bit */
7381092   case 0x6c:
739       suzy.u.data[offset+1]=0;
740       suzy.accumulate_overflow=FALSE;
741       break;
742   case 0x55: lynx_multiply();break;
743   case 0x63: lynx_divide();break;
1093      suzy.data[offset + 1] = 0;
1094      suzy.accumulate_overflow = FALSE;
1095      break;
1096   case 0x53:
1097   /* If we are going to perform a signed multiplication, we store the sign and convert the number
1098   to an unsigned one */
1099      if (suzy.data[SPRSYS] & 0x80)      /* signed math */
1100      {
1101         UINT16 factor, temp;
1102         factor = suzy.data[MATH_D] | (suzy.data[MATH_C] << 8);
1103         if ((factor - 1) & 0x8000)      /* here we use -1 to cover the math bugs on the sign of 0 and 0x8000 */
1104         {
1105            temp = (factor ^ 0xffff) + 1;
1106            sign_CD = - 1;
1107            suzy.data[MATH_D] = temp & 0xff;
1108            suzy.data[MATH_C] = temp >> 8;
1109         }
1110         else
1111            sign_CD = 1;
1112      }
1113      break;
1114   /* Writing to A will start a 16 bit multiply */
1115   /* If we are going to perform a signed multiplication, we also store the sign and convert the
1116   number to an unsigned one */
1117   case 0x55:
1118      if (suzy.data[SPRSYS] & 0x80)      /* signed math */
1119      {
1120         UINT16 factor, temp;
1121         factor = suzy.data[MATH_B] | (suzy.data[MATH_A] << 8);
1122         if ((factor - 1) & 0x8000)      /* here we use -1 to cover the math bugs on the sign of 0 and 0x8000 */
1123         {
1124            temp = (factor ^ 0xffff) + 1;
1125            sign_AB = - 1;
1126            suzy.data[MATH_B] = temp & 0xff;
1127            suzy.data[MATH_A] = temp >> 8;
1128         }
1129         else
1130            sign_AB = 1;
1131      }
1132      lynx_multiply();
1133      break;
1134   /* Writing to E will start a 16 bit divide */
1135   case 0x63:
1136      lynx_divide();
1137      break;
7441138   case 0x91:
745       if (data&1) {
746      blitter.time=timer_get_time();
747      lynx_blitter();
748       }
749//       logerror("suzy write %.2x %.2x\n",offset,data);
750       break;
751//   default:
752//       logerror("suzy write %.2x %.2x\n",offset,data);
1139      if (data & 0x01)
1140      {
1141         blitter.time = timer_get_time();
1142         lynx_blitter();
1143      }
1144      break;
1145//   case 0xb2: case 0xb3: /* Cart Bank 0 & 1 */
7531146   }
7541147}
7551148
1149
1150/****************************************
1151
1152   Mikey emulation
1153
1154****************************************/
1155
7561156/*
7571157 0xfd0a r sync signal?
7581158 0xfd81 r interrupt source bit 2 vertical refresh
r3406r3407
7641164 0xfda0-f rw farben 0..15
7651165 0xfdb0-f rw bit0..3 farben 0..15
7661166*/
1167
1168typedef struct {
1169   UINT8 data[0x100];
1170} MIKEY;
1171
7671172MIKEY mikey={ { 0 } };
7681173
7691174
1175UINT8 lynx_read_vram(UINT16 address)
1176{
1177   UINT8 result = 0x00;
1178   if (address <= 0xfbff)
1179      result = lynx_mem_0000[address - 0x0000];
1180   else if (address <= 0xfcff)
1181      result = lynx_mem_fc00[address - 0xfc00];
1182   else if (address <= 0xfdff)
1183      result = lynx_mem_fd00[address - 0xfd00];
1184   else if (address <= 0xfff7)
1185      result = lynx_mem_fe00[address - 0xfe00];
1186   else if (address >= 0xfffa)
1187      result = lynx_mem_fffa[address - 0xfffa];
1188   return result;
1189}
1190
1191
7701192/*
1193DISPCTL EQU $FD92       ; set to $D by INITMIKEY
1194
1195; B7..B4        0
1196; B3    1 EQU color
1197; B2    1 EQU 4 bit mode
1198; B1    1 EQU flip screen
1199; B0    1 EQU video DMA enabled
1200*/
1201void lynx_draw_lines(running_machine *machine, int newline)
1202{
1203   static int height = -1, width = -1;
1204   int h,w;
1205   int x, yend;
1206   UINT16 j; // clipping needed!
1207   UINT8 byte;
1208   UINT16 *line;
1209
1210   if (newline == -1)
1211      yend = 102;
1212   else
1213      yend = newline;
1214
1215   if (yend > 102)
1216      yend = 102;
1217
1218   if (yend == lynx_line_y)
1219   {
1220      if (newline == -1)
1221         lynx_line_y = 0;
1222      return;
1223   }
1224
1225   j = (mikey.data[0x94] | (mikey.data[0x95]<<8)) + lynx_line_y * 160 / 2;
1226   if (mikey.data[0x92] & 0x02)
1227      j -= 160 * 102 / 2 - 1;
1228
1229   /* rotation */
1230   if (lynx_rotate & 0x03)
1231   {
1232      h = 160; w = 102;
1233      if (((lynx_rotate == 1) && (mikey.data[0x92] & 0x02)) || ((lynx_rotate == 2) && !(mikey.data[0x92] & 0x02)))
1234      {
1235         for ( ; lynx_line_y < yend; lynx_line_y++)
1236         {
1237            line = BITMAP_ADDR16(tmpbitmap, lynx_line_y, 0);
1238            for (x = 160 - 2; x >= 0; j++, x -= 2)
1239            {
1240               byte = lynx_read_vram(j);
1241               line[x + 1] = lynx_palette[(byte >> 4) & 0x0f];
1242               line[x + 0] = lynx_palette[(byte >> 0) & 0x0f];
1243            }
1244         }
1245      }
1246      else
1247      {
1248         for ( ; lynx_line_y < yend; lynx_line_y++)
1249         {
1250            line = BITMAP_ADDR16(tmpbitmap, 102 - 1 - lynx_line_y, 0);
1251            for (x = 0; x < 160; j++, x += 2)
1252            {
1253               byte = lynx_read_vram(j);
1254               line[x + 0] = lynx_palette[(byte >> 4) & 0x0f];
1255               line[x + 1] = lynx_palette[(byte >> 0) & 0x0f];
1256            }
1257         }
1258      }
1259   }
1260   else
1261   {
1262      w = 160; h = 102;
1263      if (mikey.data[0x92] & 0x02)
1264      {
1265         for ( ; lynx_line_y < yend; lynx_line_y++)
1266         {
1267            line = BITMAP_ADDR16(tmpbitmap, 102 - 1 - lynx_line_y, 0);
1268            for (x = 160 - 2; x >= 0; j++, x -= 2)
1269            {
1270               byte = lynx_read_vram(j);
1271               line[x + 1] = lynx_palette[(byte >> 4) & 0x0f];
1272               line[x + 0] = lynx_palette[(byte >> 0) & 0x0f];
1273            }
1274         }
1275      }
1276      else
1277      {
1278         for ( ; lynx_line_y < yend; lynx_line_y++)
1279         {
1280            line = BITMAP_ADDR16(tmpbitmap, lynx_line_y, 0);
1281            for (x = 0; x < 160; j++, x += 2)
1282            {
1283               byte = lynx_read_vram(j);
1284               line[x + 0] = lynx_palette[(byte >> 4) & 0x0f];
1285               line[x + 1] = lynx_palette[(byte >> 0) & 0x0f];
1286            }
1287         }
1288      }
1289   }
1290   if (newline == -1)
1291   {
1292      lynx_line_y = 0;
1293      if ((w != width) || (h != height))
1294      {
1295         width = w;
1296         height = h;
1297         video_screen_set_visarea(machine->primary_screen, 0, width - 1, 0, height - 1);
1298      }
1299   }
1300}
1301
1302
1303/****************************************
1304
1305   Timers
1306
1307****************************************/
1308
1309/*
7711310HCOUNTER        EQU TIMER0
7721311VCOUNTER        EQU TIMER2
7731312SERIALRATE      EQU TIMER4
r3406r3407
8231362
8241363static void lynx_timer_signal_irq(running_machine *machine, int which)
8251364{
826    if ( ( lynx_timer[which].cntrl1 & 0x80 ) && ( which != 4 ) ) { // irq flag handling later
1365   if ( ( lynx_timer[which].cntrl1 & 0x80 ) && ( which != 4 ) ) { // irq flag handling later
8271366      mikey.data[0x81] |= ( 1 << which );
8281367      cpunum_set_input_line(machine, 0, M65SC02_IRQ_LINE, ASSERT_LINE);
829    }
830    switch ( which ) {
831    case 0:
1368   }
1369   switch ( which ) {
1370   case 0:
8321371      lynx_timer_count_down( machine, 2 );
8331372      lynx_line++;
8341373      break;
835    case 2:
1374   case 2:
8361375      lynx_timer_count_down( machine, 4 );
8371376      lynx_draw_lines( machine, -1 );
8381377      lynx_line=0;
8391378      break;
840    case 1:
1379   case 1:
8411380      lynx_timer_count_down( machine, 3 );
8421381      break;
843    case 3:
1382   case 3:
8441383      lynx_timer_count_down( machine, 5 );
8451384      break;
846    case 5:
1385   case 5:
8471386      lynx_timer_count_down( machine, 7 );
8481387      break;
849    case 7:
1388   case 7:
8501389      lynx_audio_count_down( machine, 0 );
8511390      break;
852    }
1391   }
8531392}
8541393
8551394void lynx_timer_count_down(running_machine *machine, int which)
8561395{
857    if ( ( lynx_timer[which].cntrl1 & 0x0f ) == 0x0f ) {
1396   if ( ( lynx_timer[which].cntrl1 & 0x0f ) == 0x0f ) {
8581397      if ( lynx_timer[which].counter > 0 ) {
859          lynx_timer[which].counter--;
860          return;
1398         lynx_timer[which].counter--;
1399         return;
8611400      }
8621401      if ( lynx_timer[which].counter == 0 ) {
8631402         lynx_timer[which].cntrl2 |= 8;
864          lynx_timer_signal_irq(machine, which);
865          if ( lynx_timer[which].cntrl1 & 0x10 ) {
1403         lynx_timer_signal_irq(machine, which);
1404         if ( lynx_timer[which].cntrl1 & 0x10 ) {
8661405            lynx_timer[which].counter = lynx_timer[which].bakup;
867          } else {
1406         } else {
8681407            lynx_timer[which].counter--;
869          }
870          return;
1408         }
1409         return;
8711410      }
872    }
1411   }
8731412}
8741413
8751414static TIMER_CALLBACK(lynx_timer_shot)
8761415{
8771416   lynx_timer[param].cntrl2 |= 8;
878    lynx_timer_signal_irq( machine, param );
879    if ( ! ( lynx_timer[param].cntrl1 & 0x10 ) )
1417   lynx_timer_signal_irq( machine, param );
1418   if ( ! ( lynx_timer[param].cntrl1 & 0x10 ) )
8801419      lynx_timer[param].timer_active = 0;
8811420}
8821421
r3406r3407
8971436
8981437static UINT8 lynx_timer_read(int which, int offset)
8991438{
900   UINT8 data = 0;
901   switch (offset) {
902   case 0:
903      data = lynx_timer[which].bakup;
904      break;
905   case 1:
906      data = lynx_timer[which].cntrl1;
907      break;
908   case 2:
909      if ( ( lynx_timer[which].cntrl1 & 7 ) == 7 )
910      {
911         data = lynx_timer[which].counter;
912      }
913      else
914      {
915         if ( lynx_timer[which].timer_active )
916            data = (UINT8) ( lynx_timer[which].bakup - attotime_mul(timer_timeleft(lynx_timer[which].timer), lynx_time_factor( lynx_timer[which].cntrl1 & 7 )).seconds);
917      }
918      break;
1439   UINT8 value = 0;
9191440
920   case 3:
921      data = lynx_timer[which].cntrl2;
922      break;
1441   switch (offset)
1442   {
1443      case 0:
1444         value = lynx_timer[which].bakup;
1445         break;
1446      case 1:
1447         value = lynx_timer[which].cntrl1;
1448         break;
1449      case 2:
1450         if ((lynx_timer[which].cntrl1 & 0x07) == 0x07)
1451         {
1452            value = lynx_timer[which].counter;
1453         }
1454         else
1455         {
1456            if ( lynx_timer[which].timer_active )
1457               value = (UINT8) ( lynx_timer[which].bakup - attotime_mul(timer_timeleft(lynx_timer[which].timer), lynx_time_factor( lynx_timer[which].cntrl1 & 0x07 )).seconds);
1458         }
1459         break;
1460
1461      case 3:
1462         value = lynx_timer[which].cntrl2;
1463         break;
9231464   }
924   logerror("timer %d read %x %.2x\n", which, offset, data);
925   return data;
1465   logerror("timer %d read %x %.2x\n", which, offset, value);
1466   return value;
9261467}
9271468
9281469static void lynx_timer_write(int which, int offset, UINT8 data)
9291470{
9301471   logerror("timer %d write %x %.2x\n", which, offset, data);
9311472
932   switch (offset) {
933   case 0:
934      lynx_timer[which].bakup = data;
935      break;
936   case 1:
937      lynx_timer[which].cntrl1 = data;
938      if ( data & 0x40 )
939         lynx_timer[which].cntrl2 &= ~8;
940      break;
941   case 2:
942//      lynx_timer[which].counter = data;
943      break;
944   case 3:
945      lynx_timer[which].cntrl2 = ( lynx_timer[which].cntrl2 & 8 ) | ( data & ~8 );
946      break;
1473   switch (offset)
1474   {
1475      case 0:
1476         lynx_timer[which].bakup = data;
1477         break;
1478      case 1:
1479         lynx_timer[which].cntrl1 = data;
1480         if (data & 0x40)
1481            lynx_timer[which].cntrl2 &= ~0x08;
1482         break;
1483      case 2:
1484//         lynx_timer[which].counter = data;   // why commented out?
1485         break;
1486      case 3:
1487         lynx_timer[which].cntrl2 = (lynx_timer[which].cntrl2 & 0x08) | (data & ~0x08);
1488         break;
9471489   }
9481490
9491491   /* Update timers */
950   if ( offset < 3 ) {
951      timer_reset( lynx_timer[which].timer, attotime_never);
1492   if ( offset < 3 )
1493   {
1494      timer_reset(lynx_timer[which].timer, attotime_never);
9521495      lynx_timer[which].timer_active = 0;
953      if ( ( lynx_timer[which].cntrl1 & 0x08 ) )
1496      if ((lynx_timer[which].cntrl1 & 0x08))      // 0x48?
9541497      {
955         if ( ( lynx_timer[which].cntrl1 & 7 ) != 7 )
1498         if ((lynx_timer[which].cntrl1 & 0x07) != 0x07)
9561499         {
957            attotime t = attotime_mul(ATTOTIME_IN_HZ( lynx_time_factor( lynx_timer[which].cntrl1 & 7 ) ), lynx_timer[which].bakup + 1 );
958            if ( lynx_timer[which].cntrl1 & 0x10 )
1500            attotime t = attotime_mul(ATTOTIME_IN_HZ(lynx_time_factor(lynx_timer[which].cntrl1 & 0x07)), lynx_timer[which].bakup + 1);
1501            if (lynx_timer[which].cntrl1 & 0x10)
9591502               timer_adjust_periodic(lynx_timer[which].timer, attotime_zero, which, t);
9601503            else
9611504               timer_adjust_oneshot(lynx_timer[which].timer, t, which);
r3406r3407
9651508   }
9661509}
9671510
1511
1512/****************************************
1513
1514   UART Emulation
1515
1516****************************************/
1517
1518
9681519static struct {
969    UINT8 serctl;
970    UINT8 data_received, data_to_send, buffer;
1520   UINT8 serctl;
1521   UINT8 data_received, data_to_send, buffer;
9711522
972    int received;
973    int sending;
974    int buffer_loaded;
1523   int received;
1524   int sending;
1525   int buffer_loaded;
9751526} uart;
9761527
9771528static void lynx_uart_reset(void)
9781529{
979    memset(&uart, 0, sizeof(uart));
1530   memset(&uart, 0, sizeof(uart));
9801531}
9811532
9821533static TIMER_CALLBACK(lynx_uart_timer)
9831534{
984    if (uart.buffer_loaded) {
985   uart.data_to_send=uart.buffer;
986   uart.buffer_loaded=FALSE;
987   timer_set(ATTOTIME_IN_USEC(11), NULL, 0, lynx_uart_timer);
988    } else {
989   uart.sending=FALSE;
990    }
1535   if (uart.buffer_loaded)
1536   {
1537      uart.data_to_send = uart.buffer;
1538      uart.buffer_loaded = FALSE;
1539      timer_set(ATTOTIME_IN_USEC(11), NULL, 0, lynx_uart_timer);
1540   }
1541   else
1542      uart.sending = FALSE;
1543
9911544//    mikey.data[0x80]|=0x10;
992    if (uart.serctl&0x80) {
993   mikey.data[0x81]|=0x10;
994   cpunum_set_input_line(machine, 0, M65SC02_IRQ_LINE, ASSERT_LINE);
1545   if (uart.serctl & 0x80)
1546   {
1547      mikey.data[0x81] |= 0x10;
1548      cpunum_set_input_line(machine, 0, M65SC02_IRQ_LINE, ASSERT_LINE);
9951549    }
9961550}
9971551
9981552static  READ8_HANDLER(lynx_uart_r)
9991553{
1000    UINT8 data=0;
1001    switch (offset) {
1002    case 0x8c:
1003   if (!uart.buffer_loaded) data|=0x80;
1004   if (uart.received) data|=0x40;
1005   if (!uart.sending) data|=0x20;
1006   break;
1007    case 0x8d:
1008   data=uart.data_received;
1009   break;
1010    }
1011    logerror("uart read %.2x %.2x\n",offset,data);
1012    return data;
1554   UINT8 value = 0x00;
1555   switch (offset)
1556   {
1557      case 0x8c:
1558         if (!uart.buffer_loaded)
1559            value |= 0x80;
1560         if (uart.received)
1561            value |= 0x40;
1562         if (!uart.sending)
1563            value |= 0x20;
1564         break;
1565
1566      case 0x8d:
1567         value = uart.data_received;
1568         break;
1569   }
1570   logerror("uart read %.2x %.2x\n", offset, value);
1571   return value;
10131572}
10141573
10151574static WRITE8_HANDLER(lynx_uart_w)
10161575{
1017    logerror("uart write %.2x %.2x\n",offset,data);
1018    switch (offset) {
1019    case 0x8c:
1020   uart.serctl=data;
1021   break;
1022    case 0x8d:
1023   if (uart.sending) {
1024       uart.buffer=data;
1025       uart.buffer_loaded=TRUE;
1026   } else {
1027       uart.sending=TRUE;
1028       uart.data_to_send=data;
1029       timer_set(ATTOTIME_IN_USEC(11), NULL, 0, lynx_uart_timer);
1576   logerror("uart write %.2x %.2x\n", offset, data);
1577   switch (offset)
1578   {
1579      case 0x8c:
1580         uart.serctl = data;
1581         break;
1582
1583      case 0x8d:
1584         if (uart.sending)
1585         {
1586            uart.buffer = data;
1587            uart.buffer_loaded = TRUE;
1588         }
1589         else
1590         {
1591            uart.sending = TRUE;
1592            uart.data_to_send = data;
1593            timer_set(ATTOTIME_IN_USEC(11), NULL, 0, lynx_uart_timer);
1594         }
1595         break;
10301596   }
1031   break;
1032    }
10331597}
10341598
1035 READ8_HANDLER(mikey_read)
1599
1600/****************************************
1601
1602   Mikey memory handlers
1603
1604****************************************/
1605
1606
1607READ8_HANDLER(mikey_read)
10361608{
1037    UINT8 data=0;
1038    switch (offset) {
1039    case 0: case 1: case 2: case 3:
1040    case 4: case 5: case 6: case 7:
1041    case 8: case 9: case 0xa: case 0xb:
1042    case 0xc: case 0xd: case 0xe: case 0xf:
1043    case 0x10: case 0x11: case 0x12: case 0x13:
1044    case 0x14: case 0x15: case 0x16: case 0x17:
1045    case 0x18: case 0x19: case 0x1a: case 0x1b:
1046    case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1047      data = lynx_timer_read( offset >> 2, offset & 3 );
1048      return data;
1049    case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1050    case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
1051    case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1052    case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
1053    case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x50:
1054      data = lynx_audio_read( offset );
1055      return data;
1056    case 0x81:
1057      data = mikey.data[offset];
1058      logerror( "mikey read %.2x %.2x\n", offset, data );
1609   UINT8 direction, value = 0x00;
1610
1611   switch (offset)
1612   {
1613   case 0x00: case 0x01: case 0x02: case 0x03:
1614   case 0x04: case 0x05: case 0x06: case 0x07:
1615   case 0x08: case 0x09: case 0x0a: case 0x0b:
1616   case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1617   case 0x10: case 0x11: case 0x12: case 0x13:
1618   case 0x14: case 0x15: case 0x16: case 0x17:
1619   case 0x18: case 0x19: case 0x1a: case 0x1b:
1620   case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1621      value = lynx_timer_read(offset >> 2, offset & 0x03);
10591622      break;
1060    case 0x8b:
1061      data = mikey.data[offset];
1062      data |= 4; // no comlynx adapter
1623
1624   case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
1625   case 0x28: case 0x29: case 0x2a: case 0x2b: case 0x2c: case 0x2d: case 0x2e: case 0x2f:
1626   case 0x30: case 0x31: case 0x32: case 0x33: case 0x34: case 0x35: case 0x36: case 0x37:
1627   case 0x38: case 0x39: case 0x3a: case 0x3b: case 0x3c: case 0x3d: case 0x3e: case 0x3f:
1628   case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x50:
1629      value = lynx_audio_read(offset);
10631630      break;
1064    case 0x8c: case 0x8d:
1065      data = lynx_uart_r(machine, offset);
1631
1632   case 0x80:
1633   case 0x81:
1634      value = mikey.data[offset];
1635      logerror( "mikey read %.2x %.2x\n", offset, value );
10661636      break;
1067    default:
1068      data = mikey.data[offset];
1069      logerror( "mikey read %.2x %.2x\n", offset, data );
1070    }
1071    return data;
1637
1638   case 0x84:
1639   case 0x85:
1640      value = 0x00;
1641      break;
1642
1643   case 0x86:
1644      value = 0x80;
1645      break;
1646
1647   case 0x88:
1648      value = 0x01;
1649      break;
1650
1651   case 0x8b:
1652      direction = mikey.data[0x8a];
1653      value |= (direction & 0x01) ? (mikey.data[offset] & 0x01) : 0x01;   // External Power input
1654      value |= (direction & 0x02) ? (mikey.data[offset] & 0x02) : 0x00;   // Cart Address Data output (0 turns cart power on)
1655      value |= (direction & 0x04) ? (mikey.data[offset] & 0x04) : 0x04;   // noexp input
1656      // REST still to implement
1657      value |= (direction & 0x08) ? (mikey.data[offset] & 0x08) : 0x00;   // rest output
1658      value |= (direction & 0x10) ? (mikey.data[offset] & 0x10) : 0x10;   // audin input
1659      /* Hack: we disable COMLynx  */
1660      value |= 0x04;
1661      /* B5, B6 & B7 are not used */
1662      break;
1663
1664   case 0x8c:
1665   case 0x8d:
1666      value = lynx_uart_r(machine, offset);
1667      break;
1668
1669   default:
1670      value = mikey.data[offset];
1671      logerror( "mikey read %.2x %.2x\n", offset, value );
1672   }
1673
1674   return value;
10721675}
10731676
10741677WRITE8_HANDLER(mikey_write)
10751678{
1076   switch (offset) {
1077   case 0: case 1: case 2: case 3:
1078   case 4: case 5: case 6: case 7:
1079   case 8: case 9: case 0xa: case 0xb:
1080   case 0xc: case 0xd: case 0xe: case 0xf:
1679   switch (offset)
1680   {
1681   case 0x00: case 0x01: case 0x02: case 0x03:
1682   case 0x04: case 0x05: case 0x06: case 0x07:
1683   case 0x08: case 0x09: case 0x0a: case 0x0b:
1684   case 0x0c: case 0x0d: case 0x0e: case 0x0f:
10811685   case 0x10: case 0x11: case 0x12: case 0x13:
10821686   case 0x14: case 0x15: case 0x16: case 0x17:
10831687   case 0x18: case 0x19: case 0x1a: case 0x1b:
10841688   case 0x1c: case 0x1d: case 0x1e: case 0x1f:
1085      lynx_timer_write( offset >> 2, offset & 3, data );
1689      lynx_timer_write(offset >> 2, offset & 3, data);
10861690      return;
10871691
10881692   case 0x20: case 0x21: case 0x22: case 0x23: case 0x24: case 0x25: case 0x26: case 0x27:
r3406r3407
10941698      return;
10951699
10961700   case 0x80:
1097      mikey.data[0x81]&=~data; // clear interrupt source
1098      logerror("mikey write %.2x %.2x\n",offset,data);
1701      mikey.data[0x81] &= ~data; // clear interrupt source
1702      logerror("mikey write %.2x %.2x\n", offset, data);
10991703      if (!mikey.data[0x81])
11001704         cpunum_set_input_line(machine, 0, M65SC02_IRQ_LINE, CLEAR_LINE);
11011705      break;
11021706
1707   /* Is this correct? */
1708   case 0x81:
1709      mikey.data[offset] |= data;
1710      break;
1711
11031712   case 0x87:
1104      mikey.data[offset]=data; //?
1105      if (data&2)
1713      mikey.data[offset] = data;
1714      if (data & 0x02)      // Power (1 = on)
11061715      {
1107         if (data&1)
1716         if (data & 0x01)   // Cart Address Strobe
11081717         {
1109            suzy.high<<=1;
1110            if (mikey.data[0x8b]&2)
1111               suzy.high|=1;
1112            suzy.low=0;
1718            suzy.high <<= 1;
1719            if (mikey.data[0x8b] & 0x02)
1720               suzy.high |= 1;
1721            suzy.high &= 0xff;
1722            suzy.low = 0;
11131723         }
11141724      }
11151725      else
11161726      {
1117         suzy.high=0;
1118         suzy.low=0;
1727         suzy.high = 0;
1728         suzy.low = 0;
11191729      }
11201730      break;
11211731
r3406r3407
11271737   case 0xa8: case 0xa9: case 0xaa: case 0xab: case 0xac: case 0xad: case 0xae: case 0xaf:
11281738   case 0xb0: case 0xb1: case 0xb2: case 0xb3: case 0xb4: case 0xb5: case 0xb6: case 0xb7:
11291739   case 0xb8: case 0xb9: case 0xba: case 0xbb: case 0xbc: case 0xbd: case 0xbe: case 0xbf:
1130      mikey.data[offset]=data;
1740      mikey.data[offset] = data;
11311741      lynx_draw_lines(machine, lynx_line);
1132#if 0
1133      palette_set_color_rgb(offset&0xf,
1134               (mikey.data[0xb0+(offset&0xf)]&0xf)<<4,
1135               (mikey.data[0xa0+(offset&0xf)]&0xf)<<4,
1136               mikey.data[0xb0+(offset&0xf)]&0xf0 );
1137#else
1138      lynx_palette[offset&0xf]=machine->pens[((mikey.data[0xb0+(offset&0xf)]&0xf))
1139         |((mikey.data[0xa0+(offset&0xf)]&0xf)<<4)
1140         |((mikey.data[0xb0+(offset&0xf)]&0xf0)<<4)];
1141#endif
1742
1743      /* RED = 0xb- & 0x0f, GREEN = 0xa- & 0x0f, BLUE = (0xb- & 0xf0) >> 4 */
1744      lynx_palette[offset & 0x0f] = machine->pens[
1745         ((mikey.data[0xb0 + (offset & 0x0f)] & 0x0f)) |
1746         ((mikey.data[0xa0 + (offset & 0x0f)] & 0x0f) << 4) |
1747         ((mikey.data[0xb0 + (offset & 0x0f)] & 0xf0) << 4)];
11421748      break;
11431749
1144    case 0x8b:case 0x90:case 0x91:
1145      mikey.data[offset]=data;
1750   /* TODO: properly implement these writes */
1751   case 0x8b:
1752      mikey.data[offset] = data;
1753      if (mikey.data[0x8a] & 0x10)
1754         logerror("Trying to enable bank 1 write. %d\n", mikey.data[offset] & 0x10);
11461755      break;
1756   
1757//   case 0x90: // SDONEACK - Suzy Done Acknowledge
1758   case 0x91: // CPUSLEEP - CPU Bus Request Disable
1759      mikey.data[offset] = data;
1760      if (!data)
1761      {
1762         /* A write of '0' to this address will reset the CPU bus request flip flop */
1763         logerror("CPUSLEEP request \n");
1764      }
1765      break;
11471766
1148    default:
1767   default:
11491768      mikey.data[offset]=data;
11501769      logerror("mikey write %.2x %.2x\n",offset,data);
11511770      break;
1152    }
1771   }
11531772}
11541773
1774/****************************************
1775
1776   Init / Config
1777
1778****************************************/
1779
11551780READ8_HANDLER( lynx_memory_config_r )
11561781{
11571782   return lynx_memory_config;
r3406r3407
11591784
11601785WRITE8_HANDLER( lynx_memory_config_w )
11611786{
1162    /* bit 7: hispeed, uses page mode accesses (4 instead of 5 cycles )
1163     * when these are safe in the cpu */
1164    lynx_memory_config = data;
1787   /* bit 7: hispeed, uses page mode accesses (4 instead of 5 cycles )
1788    * when these are safe in the cpu */
1789   lynx_memory_config = data;
11651790
11661791   memory_install_read8_handler(machine, 0,  ADDRESS_SPACE_PROGRAM, 0xfc00, 0xfcff, 0, 0, (data & 1) ? SMH_BANK1 : suzy_read);
11671792   memory_install_write8_handler(machine, 0, ADDRESS_SPACE_PROGRAM, 0xfc00, 0xfcff, 0, 0, (data & 1) ? SMH_BANK1 : suzy_write);
r3406r3407
11861811   memset(&suzy, 0, sizeof(suzy));
11871812   memset(&mikey, 0, sizeof(mikey));
11881813
1189   mikey.data[0x80] = 0;
1190   mikey.data[0x81] = 0;
1814   suzy.data[0x88]  = 0x01;
1815   suzy.data[0x90]  = 0x00;
1816   suzy.data[0x91]  = 0x00;
1817   mikey.data[0x80] = 0x00;
1818   mikey.data[0x81] = 0x00;
1819   mikey.data[0x88] = 0x01;
1820   mikey.data[0x8a] = 0x00;
1821   mikey.data[0x8c] = 0x00;
1822   mikey.data[0x90] = 0x00;
1823   mikey.data[0x92] = 0x00;
11911824
11921825   lynx_uart_reset();
11931826
r3406r3407
12251858
12261859   add_reset_callback(machine, lynx_reset);
12271860}
1861
1862
1863/****************************************
1864
1865   Image handling
1866
1867****************************************/
1868
1869
1870static void lynx_partialhash(char *dest, const unsigned char *data,
1871   unsigned long length, unsigned int functions)
1872{
1873   if (length <= 64)
1874      return;
1875   hash_compute(dest, &data[64], length - 64, functions);
1876}
1877
1878int lynx_verify_cart (char *header, int kind)
1879{
1880   if (kind)
1881   {
1882      if (strncmp("BS93", &header[6], 4))
1883      {
1884         logerror("This is not a valid Lynx image\n");
1885         return IMAGE_VERIFY_FAIL;
1886      }
1887   }
1888   else
1889   {
1890      if (strncmp("LYNX",&header[0],4))
1891      {
1892         if (!strncmp("BS93", &header[6], 4))
1893         {
1894            logerror("This image is probably a Quickload image with .lnx extension\n");
1895            logerror("Try to load it with -quickload\n");
1896         }
1897         else
1898            logerror("This is not a valid Lynx image\n");
1899         return IMAGE_VERIFY_FAIL;
1900      }
1901   }
1902
1903   return IMAGE_VERIFY_PASS;
1904}
1905
1906INTERRUPT_GEN( lynx_frame_int )
1907{
1908    lynx_rotate = rotate;
1909    if ((input_port_read(machine, "ROTATION") & 0x03) != 0x03)
1910      lynx_rotate=input_port_read(machine, "ROTATION") & 0x03;
1911}
1912
1913void lynx_crc_keyword(const device_config *image)
1914{
1915    const char *info;
1916
1917    info = image_extrainfo(image);
1918    rotate = 0;
1919
1920    if (info)
1921   {
1922      if(strcmp(info, "ROTATE90DEGREE") == 0)
1923         rotate = 1;
1924      else if (strcmp(info, "ROTATE270DEGREE") == 0)
1925         rotate = 2;
1926    }
1927}
1928
1929static DEVICE_IMAGE_LOAD( lynx_cart )
1930{
1931   UINT8 *rom = memory_region(image->machine, "user1");
1932   int size = image_length(image);
1933   const char *filetype;
1934   UINT8 header[0x40];
1935/* 64 byte header
1936   LYNX
1937   intelword lower counter size
1938   0 0 1 0
1939   32 chars name
1940   22 chars manufacturer
1941*/
1942
1943   filetype = image_filetype(image);
1944
1945   if (!mame_stricmp (filetype, "lnx"))
1946   {
1947      if (image_fread(image, header, 0x40)!=0x40)
1948         return INIT_FAIL;
1949
1950      /* Check the image */
1951      if (lynx_verify_cart((char*)header, LYNX_CART) == IMAGE_VERIFY_FAIL)
1952         return INIT_FAIL;
1953
1954      /* 2008-10 FP: According to Handy source these should be page_size_bank0. Are we using
1955      it correctly in MESS? Moreover, the next two values should be page_size_bank1. We should
1956      implement this as well */
1957      lynx_granularity = header[4] | (header[5] << 8);
1958
1959      logerror ("%s %dkb cartridge with %dbyte granularity from %s\n",
1960           header + 10, size / 1024, lynx_granularity, header + 42);
1961
1962      size -= 0x40;
1963   }
1964   else if (!mame_stricmp (filetype, "lyx"))
1965   {
1966      /* 2008-10 FP: FIXME: .lyx file don't have an header, hence they miss "lynx_granularity"
1967      (see above). What if bank 0 has to be loaded elsewhere? And what about bank 1?
1968      These should work with most .lyx files, but we need additional info on raw cart images */
1969      if (size == 0x20000)
1970         lynx_granularity = 0x0200;
1971      else if (size == 0x80000)
1972         lynx_granularity = 0x0800;
1973      else
1974         lynx_granularity = 0x0400;
1975   }
1976
1977   if (image_fread(image, rom, size) != size)
1978      return INIT_FAIL;
1979
1980   lynx_crc_keyword(image);
1981
1982   return INIT_PASS;
1983}
1984
1985void lynx_cartslot_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info)
1986{
1987   /* cartslot */
1988   switch(state)
1989   {
1990      /* --- the following bits of info are returned as 64-bit signed integers --- */
1991      case MESS_DEVINFO_INT_COUNT:                     info->i = 1; break;
1992
1993      /* --- the following bits of info are returned as pointers to data or functions --- */
1994      case MESS_DEVINFO_PTR_LOAD:                     info->load = DEVICE_IMAGE_LOAD_NAME(lynx_cart); break;
1995      case MESS_DEVINFO_PTR_PARTIAL_HASH:               info->partialhash = lynx_partialhash; break;
1996
1997      /* --- the following bits of info are returned as NULL-terminated strings --- */
1998      case MESS_DEVINFO_STR_FILE_EXTENSIONS:            strcpy(info->s = device_temp_str(), "lnx,lyx"); break;
1999
2000      default:                              cartslot_device_getinfo(devclass, state, info); break;
2001   }
2002}
src/mess/drivers/lynx.c
r3406r3407
88
99#include "driver.h"
1010#include "cpu/m6502/m6502.h"
11#include "devices/snapquik.h"
12#include "devices/cartslot.h"
1311#include "includes/lynx.h"
14#include "hash.h"
1512
13#include "devices/snapquik.h"
14
1615static QUICKLOAD_LOAD( lynx );
1716
18static int rotate=0;
19int lynx_rotate;
20static int lynx_line_y;
21UINT32 lynx_palette[0x10];
22
2317static ADDRESS_MAP_START( lynx_mem , ADDRESS_SPACE_PROGRAM, 8)
2418   AM_RANGE(0x0000, 0xfbff) AM_RAM AM_BASE(&lynx_mem_0000)
2519   AM_RANGE(0xfc00, 0xfcff) AM_RAM AM_BASE(&lynx_mem_fc00)
2620   AM_RANGE(0xfd00, 0xfdff) AM_RAM AM_BASE(&lynx_mem_fd00)
27   AM_RANGE(0xfe00, 0xfff7) AM_READWRITE( SMH_BANK3, SMH_RAM ) AM_BASE(&lynx_mem_fe00) AM_SIZE(&lynx_mem_fe00_size)
21   AM_RANGE(0xfe00, 0xfff7) AM_READWRITE(SMH_BANK3, SMH_RAM) AM_BASE(&lynx_mem_fe00) AM_SIZE(&lynx_mem_fe00_size)
2822   AM_RANGE(0xfff8, 0xfff8) AM_RAM
29   AM_RANGE(0xfff9, 0xfff9) AM_READWRITE( lynx_memory_config_r, lynx_memory_config_w )
30   AM_RANGE(0xfffa, 0xffff) AM_READWRITE( SMH_BANK4, SMH_RAM ) AM_BASE(&lynx_mem_fffa)
23   AM_RANGE(0xfff9, 0xfff9) AM_READWRITE(lynx_memory_config_r, lynx_memory_config_w)
24   AM_RANGE(0xfffa, 0xffff) AM_READWRITE(SMH_BANK4, SMH_RAM) AM_BASE(&lynx_mem_fffa)
3125ADDRESS_MAP_END
3226
3327static INPUT_PORTS_START( lynx )
3428   PORT_START("JOY")
35   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON1) PORT_NAME("A")
36   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON2) PORT_NAME("B")
37   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Opt 2") PORT_CODE(KEYCODE_2)
38   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME("Opt 1") PORT_CODE(KEYCODE_1)
39   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT)
29   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_BUTTON1 ) PORT_NAME("A")
30   PORT_BIT( 0x02, IP_ACTIVE_HIGH, IPT_BUTTON2 ) PORT_NAME("B")
31   PORT_BIT( 0x04, IP_ACTIVE_HIGH, IPT_KEYBOARD ) PORT_NAME("Opt 2") PORT_CODE(KEYCODE_2)
32   PORT_BIT( 0x08, IP_ACTIVE_HIGH, IPT_KEYBOARD ) PORT_NAME("Opt 1") PORT_CODE(KEYCODE_1)
33   PORT_BIT( 0x10, IP_ACTIVE_HIGH, IPT_JOYSTICK_RIGHT )
4034   PORT_BIT( 0x20, IP_ACTIVE_HIGH, IPT_JOYSTICK_LEFT )
4135   PORT_BIT( 0x40, IP_ACTIVE_HIGH, IPT_JOYSTICK_DOWN )
42   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP   )
36   PORT_BIT( 0x80, IP_ACTIVE_HIGH, IPT_JOYSTICK_UP )
37
4338   PORT_START("PAUSE")
4439   PORT_BIT( 0x01, IP_ACTIVE_HIGH, IPT_KEYBOARD) PORT_NAME(DEF_STR(Pause)) PORT_CODE(KEYCODE_3)
4540   // power on and power off buttons
41
4642   PORT_START("ROTATION")
47   PORT_CONFNAME ( 0x03, 3, "90 Degree Rotation")
48   PORT_CONFSETTING(   2, "Counterclockwise" )
49   PORT_CONFSETTING(   1, "Clockwise" )
50   PORT_CONFSETTING(   0, DEF_STR( None ) )
51   PORT_CONFSETTING(   3, "Crcfile" )
43   PORT_CONFNAME ( 0x03, 0x03, "90 Degree Rotation" )
44   PORT_CONFSETTING(     0x00, DEF_STR( None ) )
45   PORT_CONFSETTING(     0x01, "Clockwise" )
46   PORT_CONFSETTING(     0x02, "Counterclockwise" )
47   PORT_CONFSETTING(     0x03, "Crcfile" )
5248INPUT_PORTS_END
5349
54static INTERRUPT_GEN( lynx_frame_int )
55{
56    lynx_rotate=rotate;
57    if ((input_port_read(machine, "ROTATION")&3)!=3)
58      lynx_rotate=input_port_read(machine, "ROTATION")&3;
59}
60
61static UINT8 lynx_read_vram(UINT16 address)
62{
63   UINT8 result = 0x00;
64   if (address <= 0xfbff)
65      result = lynx_mem_0000[address - 0x0000];
66   else if (address <= 0xfcff)
67      result = lynx_mem_fc00[address - 0xfc00];
68   else if (address <= 0xfdff)
69      result = lynx_mem_fd00[address - 0xfd00];
70   else if (address <= 0xfff7)
71      result = lynx_mem_fe00[address - 0xfe00];
72   else if (address >= 0xfffa)
73      result = lynx_mem_fffa[address - 0xfffa];
74   return result;
75}
76
77/*
78DISPCTL EQU $FD92       ; set to $D by INITMIKEY
79
80; B7..B4        0
81; B3    1 EQU color
82; B2    1 EQU 4 bit mode
83; B1    1 EQU flip screen
84; B0    1 EQU video DMA enabled
85*/
86void lynx_draw_lines(running_machine *machine, int newline)
87{
88   static int height=-1, width=-1;
89   int h,w;
90   int x, yend;
91   UINT16 j; // clipping needed!
92   UINT8 byte;
93   UINT16 *line;
94
95   if (video_skip_this_frame()) newline=-1;
96
97   if (newline==-1)
98      yend = 102;
99   else
100      yend = newline;
101
102   if (yend > 102)
103      yend=102;
104
105   if (yend==lynx_line_y)
106   {
107      if (newline==-1)
108         lynx_line_y=0;
109      return;
110   }
111
112   j=(mikey.data[0x94]|(mikey.data[0x95]<<8))+lynx_line_y*160/2;
113   if (mikey.data[0x92]&2)
114      j-=160*102/2-1;
115
116   if (lynx_rotate&3)
117   {
118      /* rotation */
119      h=160; w=102;
120      if ( ((lynx_rotate==1)&&(mikey.data[0x92]&2))
121            ||( (lynx_rotate==2)&&!(mikey.data[0x92]&2)) )
122      {
123         for (;lynx_line_y<yend;lynx_line_y++)
124         {
125            line = BITMAP_ADDR16(tmpbitmap, lynx_line_y, 0);
126            for (x=160-2;x>=0;j++,x-=2)
127            {
128               byte = lynx_read_vram(j);
129               line[x + 1] = lynx_palette[(byte >> 4) & 0x0f];
130               line[x + 0] = lynx_palette[(byte >> 0) & 0x0f];
131            }
132         }
133      }
134      else
135      {
136         for (;lynx_line_y<yend;lynx_line_y++)
137         {
138            line = BITMAP_ADDR16(tmpbitmap, 102-1-lynx_line_y, 0);
139            for (x=0;x<160;j++,x+=2)
140            {
141               byte = lynx_read_vram(j);
142               line[x + 0] = lynx_palette[(byte >> 4) & 0x0f];
143               line[x + 1] = lynx_palette[(byte >> 0) & 0x0f];
144            }
145         }
146      }
147   }
148   else
149   {
150      w=160; h=102;
151      if ( mikey.data[0x92]&2)
152      {
153         for (;lynx_line_y<yend;lynx_line_y++)
154         {
155            line = BITMAP_ADDR16(tmpbitmap, 102-1-lynx_line_y, 0);
156            for (x=160-2;x>=0;j++,x-=2)
157            {
158               byte = lynx_read_vram(j);
159               line[x + 1] = lynx_palette[(byte >> 4) & 0x0f];
160               line[x + 0] = lynx_palette[(byte >> 0) & 0x0f];
161            }
162         }
163      }
164      else
165      {
166         for (;lynx_line_y<yend;lynx_line_y++)
167         {
168            line = BITMAP_ADDR16(tmpbitmap, lynx_line_y, 0);
169            for (x=0;x<160;j++,x+=2)
170            {
171               byte = lynx_read_vram(j);
172               line[x + 0] = lynx_palette[(byte >> 4) & 0x0f];
173               line[x + 1] = lynx_palette[(byte >> 0) & 0x0f];
174            }
175         }
176      }
177   }
178   if (newline==-1)
179   {
180      lynx_line_y=0;
181      if ((w!=width)||(h!=height))
182      {
183         width=w;
184         height=h;
185         video_screen_set_visarea(machine->primary_screen, 0, width-1, 0, height-1);
186      }
187   }
188}
189
19050static PALETTE_INIT( lynx )
19151{
19252    int i;
r3406r3407
22383    /* video hardware */
22484   MDRV_SCREEN_ADD("main", LCD)
22585   MDRV_SCREEN_REFRESH_RATE(LCD_FRAMES_PER_SECOND)
226   MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(2500)) /* not accurate */
86   MDRV_SCREEN_VBLANK_TIME(ATTOSECONDS_IN_USEC(1000)) /* not accurate */
22787   MDRV_SCREEN_FORMAT(BITMAP_FORMAT_INDEXED16)
22888   /*MDRV_SCREEN_SIZE(160, 102)*/
22989   MDRV_SCREEN_SIZE(160, 160)
r3406r3407
241101   MDRV_SOUND_ROUTE(ALL_OUTPUTS, "mono", 0.50)
242102
243103   /* devices */
244   MDRV_QUICKLOAD_ADD(lynx, "o", 0)
104   MDRV_QUICKLOAD_ADD(lynx, "o,lnx", 0)
245105MACHINE_DRIVER_END
246106
247107
r3406r3407
287147ROM_END
288148
289149
290
291void lynx_partialhash(char *dest, const unsigned char *data,
292   unsigned long length, unsigned int functions)
293{
294   if (length <= 64)
295      return;
296   hash_compute(dest, &data[64], length - 64, functions);
297}
298
299#define LYNX_CART      0
300#define LYNX_QUICKLOAD   1
301
302static int lynx_verify_cart (char *header, int kind)
303{
304   logerror("Trying Header Compare\n");
305
306   if (kind)
307   {
308      if (strncmp("BS93", &header[6], 4))
309      {
310         logerror("Not an valid Lynx image\n");
311         return IMAGE_VERIFY_FAIL;
312      }
313   }
314   else
315   {
316      if (strncmp("LYNX",&header[0],4))
317      {
318         logerror("Not an valid Lynx image\n");
319         return IMAGE_VERIFY_FAIL;
320      }
321   }
322
323   logerror("returning ID_OK\n");
324   return IMAGE_VERIFY_PASS;
325}
326
327static void lynx_crc_keyword(const device_config *image)
328{
329    const char *info;
330
331    info = image_extrainfo(image);
332    rotate = 0;
333
334    if (info)
335   {
336      if(strcmp(info, "ROTATE90DEGREE")==0)
337         rotate = 1;
338      else if (strcmp(info, "ROTATE270DEGREE")==0)
339         rotate = 2;
340    }
341}
342
343static DEVICE_IMAGE_LOAD( lynx_cart )
344{
345   UINT8 *rom = memory_region(image->machine, "user1");
346   int size = image_length(image);
347   const char *filetype;
348   UINT8 header[0x40];
349/* 64 byte header
350   LYNX
351   intelword lower counter size
352   0 0 1 0
353   32 chars name
354   22 chars manufacturer
355*/
356
357   filetype = image_filetype(image);
358
359   if (!mame_stricmp (filetype, "lnx"))
360   {
361      if (image_fread(image, header, 0x40)!=0x40)
362         return INIT_FAIL;
363
364      /* Check the image */
365      if (lynx_verify_cart((char*)header, LYNX_CART) == IMAGE_VERIFY_FAIL)
366         return INIT_FAIL;
367
368      /* 2008-10 FP: According to Handy source these should be page_size_bank0. Are we using
369      it correctly in MESS? Moreover, the next two values should be page_size_bank1. We should
370      implement this as well */
371      lynx_granularity = header[4] | (header[5] << 8);
372
373      logerror ("%s %dkb cartridge with %dbyte granularity from %s\n",
374           header + 10, size / 1024, lynx_granularity, header + 42);
375
376      size -= 0x40;
377   }
378   else if (!mame_stricmp (filetype, "lyx"))
379   {
380      /* 2008-10 FP: FIXME: .lyx file don't have an header, hence they miss "lynx_granularity"
381      (see above). What if bank 0 has to be loaded elsewhere? And what about bank 1?
382      These should work with most .lyx files, but we need additional info on raw cart images */
383      if (size == 0x20000)
384         lynx_granularity = 0x0200;
385      else if (size == 0x80000)
386         lynx_granularity = 0x0800;
387      else
388         lynx_granularity = 0x0400;
389   }
390
391   if (image_fread(image, rom, size) != size)
392      return INIT_FAIL;
393
394   lynx_crc_keyword(image);
395
396   return INIT_PASS;
397}
398
399150static QUICKLOAD_LOAD( lynx )
400151{
401152   UINT8 *data = NULL;
r3406r3407
431182   return INIT_PASS;
432183}
433184
434static void lynx_cartslot_getinfo(const mess_device_class *devclass, UINT32 state, union devinfo *info)
435{
436   /* cartslot */
437   switch(state)
438   {
439      /* --- the following bits of info are returned as 64-bit signed integers --- */
440      case MESS_DEVINFO_INT_COUNT:                     info->i = 1; break;
441
442      /* --- the following bits of info are returned as pointers to data or functions --- */
443      case MESS_DEVINFO_PTR_LOAD:                     info->load = DEVICE_IMAGE_LOAD_NAME(lynx_cart); break;
444      case MESS_DEVINFO_PTR_PARTIAL_HASH:               info->partialhash = lynx_partialhash; break;
445
446      /* --- the following bits of info are returned as NULL-terminated strings --- */
447      case MESS_DEVINFO_STR_FILE_EXTENSIONS:            strcpy(info->s = device_temp_str(), "lnx,lyx"); break;
448
449      default:                              cartslot_device_getinfo(devclass, state, info); break;
450   }
451}
452
453185static SYSTEM_CONFIG_START(lynx)
454186   CONFIG_DEVICE(lynx_cartslot_getinfo)
455187SYSTEM_CONFIG_END

Previous 15994 Revisions Next


© 1998-2018 The MESS Team