Previous 15994 Revisions Next

r1285 Thursday 15th November, 2007 at 22:05:56 UTC by Wilbert Pol
[SMSVDP] Internal update: Moved globals into a struct. Removed some unneeded variables. Changed some functions into static functions.
[src/mess/video]smsvdp.c

src/mess/video/smsvdp.c
r1284r1285
4141#include "includes/sms.h"
4242#include "video/generic.h"
4343
44#define IS_SMS1_VDP      ( features & MODEL_315_5124 )
45#define IS_SMS2_VDP      ( features & MODEL_315_5246 )
46#define IS_GAMEGEAR_VDP      ( features & MODEL_315_5378 )
44#define IS_SMS1_VDP         ( smsvdp.features & MODEL_315_5124 )
45#define IS_SMS2_VDP         ( smsvdp.features & MODEL_315_5246 )
46#define IS_GAMEGEAR_VDP      ( smsvdp.features & MODEL_315_5378 )
4747
48#define STATUS_VINT      (0x80)   /* Pending vertical interrupt flag */
48#define STATUS_VINT         (0x80)   /* Pending vertical interrupt flag */
4949#define STATUS_SPROVR      (0x40)   /* Sprite overflow flag */
5050#define STATUS_SPRCOL      (0x20)   /* Object collision flag */
51#define STATUS_HINT      (0x02)   /* Pending horizontal interrupt flag */
51#define STATUS_HINT         (0x02)   /* Pending horizontal interrupt flag */
5252
5353#define GG_CRAM_SIZE      (0x40)   /* 32 colors x 2 bytes per color = 64 bytes */
5454#define SMS_CRAM_SIZE      (0x20)   /* 32 colors x 1 bytes per color = 32 bytes */
5555#define MAX_CRAM_SIZE      0x40
5656
57#define VRAM_SIZE      (0x4000)
57#define VRAM_SIZE         (0x4000)
5858
5959#define PRIORITY_BIT      0x1000
60#define BACKDROP_COLOR      ( ( vdp_mode == 4 ? 0x10 : 0x00 ) + (reg[0x07] & 0x0F))
60#define BACKDROP_COLOR      ( ( smsvdp.vdp_mode == 4 ? 0x10 : 0x00 ) + (smsvdp.reg[0x07] & 0x0F))
6161
6262#define NUM_OF_REGISTER      (0x10)  /* 16 registers */
6363
64#define INIT_VCOUNT     
64#define INIT_VCOUNT         
6565#define VERTICAL_BLANKING   1
6666#define TOP_BLANKING      2
67#define TOP_BORDER      3
67#define TOP_BORDER         3
6868#define ACTIVE_DISPLAY_V   4
6969#define BOTTOM_BORDER      5
7070#define BOTTOM_BLANKING      6
7171
72static UINT8 sms_ntsc_192[7] = { 0xD5, 3, 13, 27, 192, 24, 3 };
73static UINT8 sms_ntsc_224[7] = { 0xE5, 3, 13, 11, 224,  8, 3 };
74static UINT8 sms_ntsc_240[7] = { 0xEE, 3, 13,  3, 240,  0, 3 };
75static UINT8 sms_pal_192[7]  = { 0xBA, 3, 13, 54, 192, 48, 3 };
76static UINT8 sms_pal_224[7]  = { 0xCA, 3, 13, 38, 224, 32, 3 };
77static UINT8 sms_pal_240[7]  = { 0xD2, 3, 13, 30, 240, 24, 3 };
72static const UINT8 sms_ntsc_192[7] = { 0xD5, 3, 13, 27, 192, 24, 3 };
73static const UINT8 sms_ntsc_224[7] = { 0xE5, 3, 13, 11, 224,  8, 3 };
74static const UINT8 sms_ntsc_240[7] = { 0xEE, 3, 13,  3, 240,  0, 3 };
75static const UINT8 sms_pal_192[7]  = { 0xBA, 3, 13, 54, 192, 48, 3 };
76static const UINT8 sms_pal_224[7]  = { 0xCA, 3, 13, 38, 224, 32, 3 };
77static const UINT8 sms_pal_240[7]  = { 0xD2, 3, 13, 30, 240, 24, 3 };
7878
79UINT8 *sms_frame_timing;
79static struct _smsvdp {
80   UINT32      features;
81   UINT8      reg[NUM_OF_REGISTER];      /* All the registers */
82   UINT8      status;                  /* Status register */
83   UINT8      reg9copy;               /* Internal copy of register 9 */
84   UINT8      addrmode;               /* Type of VDP action */
85   UINT16      addr;                  /* Contents of internal VDP address register */
86   UINT8      cram_mask;               /* Mask to switch between SMS and GG CRAM sizes */
87   int         cram_dirty;               /* Have there been any changes to the CRAM area */
88   int         pending;
89   UINT8      buffer;
90   int         gg_sms_mode;            /* Shrunk SMS screen on GG lcd mode flag */
91   int         irq_state;               /* The status of the IRQ line of the VDP */
92   int         vdp_mode;               /* Current mode of the VDP: 0,1,2,3,4 */
93   int         y_pixels;               /* 192, 224, 240 */
94   int         line_counter;
95   UINT8      *VRAM;                  /* Pointer to VRAM */
96   UINT8      *CRAM;                  /* Pointer to CRAM */
97   const UINT8   *sms_frame_timing;
98   mame_bitmap   *prev_bitmap;
99   int         prev_bitmap_saved;
100   UINT8      *collision_buffer;
80101
81UINT8 reg[NUM_OF_REGISTER];
82UINT8 *VRAM = NULL;
83UINT8 *CRAM = NULL;
84int CRAMMask;
85UINT8 *lineCollisionBuffer;
86UINT8 reg9copy;
102   /* lineBuffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
103      Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
104      sms compatibility mode.
105    */
106   int         *line_buffer;
107   int         current_palette[32];
108   void (*int_callback)(int);
109   mame_timer   *smsvdp_display_timer;
110} smsvdp;
87111
88int addr;
89int code;
90int pending;
91int latch;
92int buffer;
93int statusReg;
94int ggSmsMode;
95UINT32 features;
96
97int isCRAMDirty;
98
99int lineCountDownCounter;
100int irqState;         /* The status of the IRQ line, as seen by the VDP */
101int y_pixels;         /* 192, 224, 240 */
102int vdp_mode;         /* current mode of the VDP: 0,1,2,3,4 */
103
104mame_bitmap *prevBitMap;
105
106/* lineBuffer will be used to hold 5 lines of line data. Line #0 is the regular blitting area.
107   Lines #1-#4 will be used as a kind of cache to be used for vertical scaling in the gamegear
108   sms compatibility mode.
109 */
110int *lineBuffer = NULL;
111int currentPalette[32];
112int prevBitMapSaved;
113void (*int_callback)(int);
114mame_timer *smsvdp_display_timer = NULL;
115
116112static TIMER_CALLBACK(smsvdp_display_callback);
117void sms_refresh_line(mame_bitmap *bitmap, int offsetx, int offsety, int line);
118void sms_update_palette(void);
113static void sms_refresh_line(mame_bitmap *bitmap, int offsetx, int offsety, int line);
114static void sms_update_palette(void);
119115
120116static void set_display_settings( void ) {
121117   int M1, M2, M3, M4;
122   M1 = reg[0x01] & 0x10;
123   M2 = reg[0x00] & 0x02;
124   M3 = reg[0x01] & 0x08;
125   M4 = reg[0x00] & 0x04;
126   y_pixels = 192;
118   M1 = smsvdp.reg[0x01] & 0x10;
119   M2 = smsvdp.reg[0x00] & 0x02;
120   M3 = smsvdp.reg[0x01] & 0x08;
121   M4 = smsvdp.reg[0x00] & 0x04;
122   smsvdp.y_pixels = 192;
127123   if ( M4 ) {
128124      /* mode 4 */
129      vdp_mode = 4;
125      smsvdp.vdp_mode = 4;
130126      if ( M2 && ( IS_SMS2_VDP || IS_GAMEGEAR_VDP ) ) {
131127         /* Is it 224-line display */
132128         if ( M1 && ! M3 ) {
133            y_pixels = 224;
129            smsvdp.y_pixels = 224;
134130         } else if ( ! M1 && M3 ) {
135131            /* 240-line display */
136            y_pixels = 240;
132            smsvdp.y_pixels = 240;
137133         }
138134      }
139135   } else {
140136      /* original TMS9918 mode */
141137      if ( ! M1 && ! M2 && ! M3 ) {
142         vdp_mode = 0;
138         smsvdp.vdp_mode = 0;
143139      } else
144140//      if ( M1 && ! M2 && ! M3 ) {
145//         vdp_mode = 1;
141//         smsvdp.vdp_mode = 1;
146142//      } else
147143      if ( ! M1 && M2 && ! M3 ) {
148         vdp_mode = 2;
144         smsvdp.vdp_mode = 2;
149145//      } else
150146//      if ( ! M1 && ! M2 && M3 ) {
151//         vdp_mode = 3;
147//         smsvdp.vdp_mode = 3;
152148      } else {
153149         logerror( "Unknown video mode detected (M1=%c, M2=%c, M3=%c, M4=%c)\n", M1 ? '1' : '0', M2 ? '1' : '0', M3 ? '1' : '0', M4 ? '1' : '0');
154150      }
155151   }
156   switch( y_pixels ) {
152   switch( smsvdp.y_pixels ) {
157153   case 192:
158      sms_frame_timing = ( Machine->screen[0].height == PAL_Y_PIXELS ) ? sms_pal_192 : sms_ntsc_192;
154      smsvdp.sms_frame_timing = ( Machine->screen[0].height == PAL_Y_PIXELS ) ? sms_pal_192 : sms_ntsc_192;
159155      break;
160156   case 224:
161      sms_frame_timing = ( Machine->screen[0].height == PAL_Y_PIXELS ) ? sms_pal_224 : sms_ntsc_224;
157      smsvdp.sms_frame_timing = ( Machine->screen[0].height == PAL_Y_PIXELS ) ? sms_pal_224 : sms_ntsc_224;
162158      break;
163159   case 240:
164      sms_frame_timing = ( Machine->screen[0].height == PAL_Y_PIXELS ) ? sms_pal_240 : sms_ntsc_240;
160      smsvdp.sms_frame_timing = ( Machine->screen[0].height == PAL_Y_PIXELS ) ? sms_pal_240 : sms_ntsc_240;
165161      break;
166162   }
167   isCRAMDirty = 1;
163   smsvdp.cram_dirty = 1;
168164}
169165
170166 READ8_HANDLER(sms_vdp_vcount_r) {
171   return ( sms_frame_timing[ INIT_VCOUNT ] + video_screen_get_vpos(0) ) & 0xFF;
167   return ( smsvdp.sms_frame_timing[ INIT_VCOUNT ] + video_screen_get_vpos(0) ) & 0xFF;
172168}
173169
174170READ8_HANDLER(sms_vdp_hcount_r) {
r1284r1285
176172}
177173
178174void sms_set_ggsmsmode( int mode ) {
179   ggSmsMode = mode;
175   smsvdp.gg_sms_mode = mode;
180176}
181177
182178int smsvdp_video_init( const smsvdp_configuration *config ) {
183179
184   features = config->model;
185   int_callback = config->int_callback;
180   memset( &smsvdp, 0, sizeof smsvdp );
186181
182   smsvdp.features = config->model;
183   smsvdp.int_callback = config->int_callback;
184
187185   /* Allocate video RAM
188186      In theory the driver could have a REGION_GFX1 and/or REGION_GFX2 memory region
189187      of it's own. So this code could potentially cause a clash.
190188   */
191   VRAM = new_memory_region( Machine, REGION_GFX1, VRAM_SIZE, ROM_REQUIRED );
192   CRAM = new_memory_region( Machine, REGION_GFX2, MAX_CRAM_SIZE, ROM_REQUIRED );
193   lineBuffer = auto_malloc( 256 * 5 * sizeof(int) );
194   memset( lineBuffer, 0, 256 * 5 * sizeof(int) );
189   smsvdp.VRAM = new_memory_region( Machine, REGION_GFX1, VRAM_SIZE, ROM_REQUIRED );
190   smsvdp.CRAM = new_memory_region( Machine, REGION_GFX2, MAX_CRAM_SIZE, ROM_REQUIRED );
191   smsvdp.line_buffer = auto_malloc( 256 * 5 * sizeof(int) );
192   memset( smsvdp.line_buffer, 0, 256 * 5 * sizeof(int) );
195193
196194   /* Clear RAM */
197   memset(reg, 0, NUM_OF_REGISTER);
198   isCRAMDirty = 1;
199   memset(VRAM, 0, VRAM_SIZE);
200   memset(CRAM, 0, MAX_CRAM_SIZE);
201   reg[0x01] |= 0x20;
202   reg[0x02] = 0x0E;         /* power up default */
203   reg[0x0a] = 0xff;
195   smsvdp.cram_dirty = 1;
196   memset(smsvdp.VRAM, 0, VRAM_SIZE);
197   memset(smsvdp.CRAM, 0, MAX_CRAM_SIZE);
198   smsvdp.reg[0x01] |= 0x20;
199   smsvdp.reg[0x02] = 0x0E;         /* power up default */
200   smsvdp.reg[0x0a] = 0xff;
204201
205   CRAMMask = ( IS_GAMEGEAR_VDP && ! ggSmsMode ) ? ( GG_CRAM_SIZE - 1 ) : ( SMS_CRAM_SIZE - 1 );
202   smsvdp.cram_mask = ( IS_GAMEGEAR_VDP && ! smsvdp.gg_sms_mode ) ? ( GG_CRAM_SIZE - 1 ) : ( SMS_CRAM_SIZE - 1 );
206203
207   /* Initialize VDP state variables */
208   addr = code = pending = latch = buffer = statusReg = \
209   lineCountDownCounter = irqState = 0;
204   smsvdp.collision_buffer = auto_malloc(SMS_X_PIXELS);
210205
211   lineCollisionBuffer = auto_malloc(SMS_X_PIXELS);
212
213206   /* Make temp bitmap for rendering */
214207   tmpbitmap = auto_bitmap_alloc(Machine->screen[0].width, Machine->screen[0].height, BITMAP_FORMAT_INDEXED32);
215208
216   prevBitMapSaved = 0;
217   prevBitMap = auto_bitmap_alloc(Machine->screen[0].width, Machine->screen[0].height, BITMAP_FORMAT_INDEXED32);
209   smsvdp.prev_bitmap = auto_bitmap_alloc(Machine->screen[0].width, Machine->screen[0].height, BITMAP_FORMAT_INDEXED32);
218210
219211   set_display_settings();
220212
221   smsvdp_display_timer = mame_timer_alloc( smsvdp_display_callback );
222   mame_timer_adjust( smsvdp_display_timer, video_screen_get_time_until_pos( 0, 0, 0 ), 0, video_screen_get_scan_period( 0 ) );
223   return (0);
213   smsvdp.smsvdp_display_timer = mame_timer_alloc( smsvdp_display_callback );
214   mame_timer_adjust( smsvdp.smsvdp_display_timer, video_screen_get_time_until_pos( 0, 0, 0 ), 0, video_screen_get_scan_period( 0 ) );
215   return 0;
224216}
225217
226218static TIMER_CALLBACK(smsvdp_set_irq) {
227   irqState = 1;
228   if ( int_callback ) {
229      int_callback( ASSERT_LINE );
219   smsvdp.irq_state = 1;
220   if ( smsvdp.int_callback ) {
221      smsvdp.int_callback( ASSERT_LINE );
230222   }
231223}
232224
r1284r1285
234226{
235227   rectangle rec;
236228   int vpos = video_screen_get_vpos(0);
237   int vpos_limit = sms_frame_timing[VERTICAL_BLANKING] + sms_frame_timing[TOP_BLANKING]
238                  + sms_frame_timing[TOP_BORDER] + sms_frame_timing[ACTIVE_DISPLAY_V]
239                  + sms_frame_timing[BOTTOM_BORDER] + sms_frame_timing[BOTTOM_BLANKING];
229   int vpos_limit = smsvdp.sms_frame_timing[VERTICAL_BLANKING] + smsvdp.sms_frame_timing[TOP_BLANKING]
230                  + smsvdp.sms_frame_timing[TOP_BORDER] + smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V]
231                  + smsvdp.sms_frame_timing[BOTTOM_BORDER] + smsvdp.sms_frame_timing[BOTTOM_BLANKING];
240232
241233   rec.min_y = rec.max_y = vpos;
242234
r1284r1285
246238      return;
247239   }
248240
249   vpos_limit -= sms_frame_timing[BOTTOM_BLANKING];
241   vpos_limit -= smsvdp.sms_frame_timing[BOTTOM_BLANKING];
250242
251243   /* Check if we're below the bottom border */
252244   if ( vpos >= vpos_limit ) {
253245      return;
254246   }
255247
256   vpos_limit -= sms_frame_timing[BOTTOM_BORDER];
248   vpos_limit -= smsvdp.sms_frame_timing[BOTTOM_BORDER];
257249
258250   /* Check if we're in the bottom border area */
259251   if ( vpos >= vpos_limit ) {
260252      if ( vpos == vpos_limit ) {
261         if ( lineCountDownCounter == 0x00 ) {
262            lineCountDownCounter = reg[0x0A];
263            statusReg |= STATUS_HINT;
264            if ( reg[0x00] & 0x10 ) {
253         if ( smsvdp.line_counter == 0x00 ) {
254            smsvdp.line_counter = smsvdp.reg[0x0A];
255            smsvdp.status |= STATUS_HINT;
256            if ( smsvdp.reg[0x00] & 0x10 ) {
265257               /* Delay triggering of interrupt to allow software to read the status bit before the irq */
266258               mame_timer_set( video_screen_get_time_until_pos( 0, video_screen_get_vpos(0), video_screen_get_hpos(0) + 1 ), 0, smsvdp_set_irq );
267259            }
r1284r1285
269261
270262      }
271263      if ( vpos == vpos_limit + 1 ) {
272         statusReg |= STATUS_VINT;
273         if ( reg[0x01] & 0x20 ) {
264         smsvdp.status |= STATUS_VINT;
265         if ( smsvdp.reg[0x01] & 0x20 ) {
274266            /* Delay triggering of interrupt to allow software to read the status bit before the irq */
275267            mame_timer_set( video_screen_get_time_until_pos( 0, video_screen_get_vpos(0), video_screen_get_hpos(0) + 1 ), 0, smsvdp_set_irq );
276268         }
r1284r1285
282274      /* Draw left border */
283275      rec.min_x = LBORDER_START;
284276      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
285      fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
277      fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
286278      /* Draw right border */
287279      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
288280      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
289      fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
281      fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
290282      /* Draw middle of the border */
291283      /* We need to do this through the regular drawing function so it will */
292284      /* be included in the gamegear scaling functions */
293      sms_refresh_line( tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit - sms_frame_timing[ACTIVE_DISPLAY_V], vpos - ( vpos_limit - sms_frame_timing[ACTIVE_DISPLAY_V] ) );
285      sms_refresh_line( tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit - smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V], vpos - ( vpos_limit - smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V] ) );
294286      return;
295287   }
296288
297   vpos_limit -= sms_frame_timing[ACTIVE_DISPLAY_V];
289   vpos_limit -= smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V];
298290
299291   /* Check if we're in the active display area */
300292   if ( vpos >= vpos_limit ) {
301293      if ( vpos == vpos_limit ) {
302         lineCountDownCounter = reg[0x0A];
303         reg9copy = reg[0x09];
294         smsvdp.line_counter = smsvdp.reg[0x0A];
295         smsvdp.reg9copy = smsvdp.reg[0x09];
304296      }
305      if ( lineCountDownCounter == 0x00 ) {
306         lineCountDownCounter = reg[0x0A];
307         statusReg |= STATUS_HINT;
308         if ( reg[0x00] & 0x10 ) {
297      if ( smsvdp.line_counter == 0x00 ) {
298         smsvdp.line_counter = smsvdp.reg[0x0A];
299         smsvdp.status |= STATUS_HINT;
300         if ( smsvdp.reg[0x00] & 0x10 ) {
309301            /* Delay triggering of interrupt to allow software to read the status bit before the irq */
310302            mame_timer_set( video_screen_get_time_until_pos( 0, video_screen_get_vpos(0), video_screen_get_hpos(0) + 1 ), 0, smsvdp_set_irq );
311303         }
312304      } else {
313         lineCountDownCounter -= 1;
305         smsvdp.line_counter -= 1;
314306      }
315307      if ( video_skip_this_frame() ) {
316308         return;
r1284r1285
318310      sms_update_palette();
319311
320312      /* Check if display is disabled */
321      if ( ! ( reg[0x01] & 0x40 ) ) {
313      if ( ! ( smsvdp.reg[0x01] & 0x40 ) ) {
322314         /* set whole line to backdrop color */
323315         rec.min_x = LBORDER_START;
324316         rec.max_x = LBORDER_START + LBORDER_X_PIXELS + 255 + RBORDER_X_PIXELS;
325         fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
317         fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
326318      } else {
327319         /* Draw left border */
328320         rec.min_x = LBORDER_START;
329321         rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
330         fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
322         fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
331323         /* Draw right border */
332324         rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
333325         rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
334         fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
326         fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
335327         sms_refresh_line( tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit, vpos - vpos_limit );
336328      }
337329      return;
338330   }
339331
340   vpos_limit -= sms_frame_timing[TOP_BORDER];
332   vpos_limit -= smsvdp.sms_frame_timing[TOP_BORDER];
341333
342334   /* Check if we're in the top border area */
343335   if ( vpos >= vpos_limit ) {
r1284r1285
348340      /* Draw left border */
349341      rec.min_x = LBORDER_START;
350342      rec.max_x = LBORDER_START + LBORDER_X_PIXELS - 1;
351      fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
343      fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
352344      /* Draw right border */
353345      rec.min_x = LBORDER_START + LBORDER_X_PIXELS + 256;
354346      rec.max_x = rec.min_x + RBORDER_X_PIXELS - 1;
355      fillbitmap( tmpbitmap, Machine->pens[currentPalette[BACKDROP_COLOR]], &rec );
347      fillbitmap( tmpbitmap, Machine->pens[smsvdp.current_palette[BACKDROP_COLOR]], &rec );
356348      /* Draw middle of the border */
357349      /* We need to do this through the regular drawing function so it will */
358350      /* be included in the gamegear scaling functions */
359      sms_refresh_line( tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit + sms_frame_timing[TOP_BORDER], vpos - ( vpos_limit + sms_frame_timing[TOP_BORDER] ) );
351      sms_refresh_line( tmpbitmap, LBORDER_START + LBORDER_X_PIXELS, vpos_limit + smsvdp.sms_frame_timing[TOP_BORDER], vpos - ( vpos_limit + smsvdp.sms_frame_timing[TOP_BORDER] ) );
360352      return;
361353   }
362354}
363355
364356 READ8_HANDLER(sms_vdp_data_r) {
365   int temp;
357   UINT8 temp;
366358
367359   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
368360   /* to the address register when in the middle of doing a command.               */
369361   /* Cosmic Spacehead needs this, among others                                    */
370   if ( pending ) {
371      addr = ( addr & 0xff00 ) | latch;
372      /* Clear pending write flag */
373      pending = 0;
374   }
362   /* Clear pending write flag */
363   smsvdp.pending = 0;
375364
376365   /* Return read buffer contents */
377   temp = buffer;
366   temp = smsvdp.buffer;
378367
379368   /* Load read buffer */
380   buffer = VRAM[(addr & 0x3FFF)];
369   smsvdp.buffer = smsvdp.VRAM[(smsvdp.addr & 0x3FFF)];
381370
382371   /* Bump internal address register */
383   addr += 1;
372   smsvdp.addr += 1;
384373   return (temp);
385374}
386375
387376 READ8_HANDLER(sms_vdp_ctrl_r) {
388   int temp = statusReg;
377   UINT8 temp = smsvdp.status;
389378
390379   /* Clear pending write flag */
391   pending = 0;
380   smsvdp.pending = 0;
392381
393   statusReg &= ~( STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT );
382   smsvdp.status &= ~( STATUS_VINT | STATUS_SPROVR | STATUS_SPRCOL | STATUS_HINT );
394383
395   if (irqState == 1) {
396      irqState = 0;
397      if ( int_callback ) {
398         int_callback( CLEAR_LINE );
384   if (smsvdp.irq_state == 1) {
385      smsvdp.irq_state = 0;
386      if ( smsvdp.int_callback ) {
387         smsvdp.int_callback( CLEAR_LINE );
399388      }
400389   }
401390
r1284r1285
405394WRITE8_HANDLER(sms_vdp_data_w) {
406395   /* SMS 2 & GG behaviour. Seems like the latched data is passed straight through */
407396   /* to the address register when in the middle of doing a command.               */
408        /* Cosmic Spacehead needs this, among others                                    */
409   if ( pending ) {
410      addr = ( addr & 0xff00 ) | latch;
411      /* Clear pending write flag */
412      pending = 0;
413   }
397   /* Cosmic Spacehead needs this, among others                                    */
398   /* Clear pending write flag */
399   smsvdp.pending = 0;
414400
415   switch(code) {
401   switch(smsvdp.addrmode) {
416402      case 0x00:
417403      case 0x01:
418404      case 0x02: {
419         int address = (addr & 0x3FFF);
405         int address = (smsvdp.addr & 0x3FFF);
420406
421         VRAM[address] = data;
407         smsvdp.VRAM[address] = data;
422408      }
423409      break;
424410      case 0x03: {
425         int address = addr & CRAMMask;
411         int address = smsvdp.addr & smsvdp.cram_mask;
426412
427         if (data != CRAM[address]) {
428            CRAM[address] = data;
429            isCRAMDirty = 1;
413         if (data != smsvdp.CRAM[address]) {
414            smsvdp.CRAM[address] = data;
415            smsvdp.cram_dirty = 1;
430416         }
431417      }
432418      break;
433419   }
434420
435   buffer = data;
436   addr += 1;
421   smsvdp.buffer = data;
422   smsvdp.addr += 1;
437423}
438424
439425WRITE8_HANDLER(sms_vdp_ctrl_w) {
440426   int regNum;
441427
442   if (pending == 0) {
443      latch = data;
444      pending = 1;
428   if (smsvdp.pending == 0) {
429      smsvdp.addr = ( smsvdp.addr & 0xff00 ) | data;
430      smsvdp.pending = 1;
445431   } else {
446432      /* Clear pending write flag */
447      pending = 0;
433      smsvdp.pending = 0;
448434
449      code = (data >> 6) & 0x03;
450      addr = ( data << 8 ) | latch;
451      switch( code ) {
435      smsvdp.addrmode = (data >> 6) & 0x03;
436      smsvdp.addr = ( data << 8 ) | ( smsvdp.addr & 0xff );
437      switch( smsvdp.addrmode ) {
452438      case 0:      /* VRAM reading mode */
453         buffer = VRAM[ addr & 0x3FFF ];
454         addr += 1;
439         smsvdp.buffer = smsvdp.VRAM[ smsvdp.addr & 0x3FFF ];
440         smsvdp.addr += 1;
455441         break;
456442      case 1:      /* VRAM writing mode */
457443         break;
458444      case 2:      /* VDP register write */
459445         regNum = data & 0x0F;
460         reg[regNum] = latch;
461         if (regNum == 0 && latch & 0x02) {
446         smsvdp.reg[regNum] = smsvdp.addr & 0xff;
447         if (regNum == 0 && smsvdp.addr & 0x02) {
462448            logerror("overscan enabled.\n");
463449         }
464450         if ( regNum == 0 || regNum == 1 ) {
465451            set_display_settings();
466452         }
467         if ( ( regNum == 1 ) && ( reg[0x01] & 0x20 ) && ( statusReg & STATUS_VINT ) ) {
468            int_callback( ASSERT_LINE );
453         if ( ( regNum == 1 ) && ( smsvdp.reg[0x01] & 0x20 ) && ( smsvdp.status & STATUS_VINT ) ) {
454            smsvdp.int_callback( ASSERT_LINE );
469455         }
470         code = 0;
456         smsvdp.addrmode = 0;
471457         break;
472458      case 3:      /* CRAM writing mode */
473459         break;
r1284r1285
475461   }
476462}
477463
478void sms_refresh_line_mode4(int *lineBuffer, int line) {
464static void sms_refresh_line_mode4(int *lineBuffer, int line) {
479465   int tileColumn;
480466   int xScroll, yScroll, xScrollStartColumn;
481467   int spriteIndex;
r1284r1285
485471   int bitPlane0, bitPlane1, bitPlane2, bitPlane3;
486472   UINT16 nameTableAddress;
487473   UINT8 *nameTable;
488   UINT8 *spriteTable = &VRAM[(reg[0x05] << 7) & 0x3F00];
474   UINT8 *spriteTable = smsvdp.VRAM + ( (smsvdp.reg[0x05] << 7) & 0x3F00 );
489475
490   if ( y_pixels != 192 ) {
491      nameTableAddress = (((reg[0x02] & 0x0C) << 10) | 0x0700) + ((((line + reg9copy) % 256) >> 3) << 6);
476   if ( smsvdp.y_pixels != 192 ) {
477      nameTableAddress = (((smsvdp.reg[0x02] & 0x0C) << 10) | 0x0700) + ((((line + smsvdp.reg9copy) % 256) >> 3) << 6);
492478   } else {
493      nameTableAddress = ((reg[0x02] << 10) & 0x3800) + ((((line + reg9copy) % 224) >> 3) << 6);
479      nameTableAddress = ((smsvdp.reg[0x02] << 10) & 0x3800) + ((((line + smsvdp.reg9copy) % 224) >> 3) << 6);
494480   }
495481   if ( IS_SMS1_VDP ) {
496      nameTableAddress = nameTableAddress & (((reg[0x02] & 0x01) << 10) | 0x3BFF);
482      nameTableAddress = nameTableAddress & (((smsvdp.reg[0x02] & 0x01) << 10) | 0x3BFF);
497483   }
498   nameTable = VRAM + nameTableAddress;
484   nameTable = smsvdp.VRAM + nameTableAddress;
499485
500486   /* if top 2 rows of screen not affected by horizontal scrolling, then xScroll = 0 */
501487   /* else xScroll = reg[0x08] (SMS Only)                                            */
502488   if ( IS_GAMEGEAR_VDP ) {
503      xScroll = 0x0100 - reg[0x08];
489      xScroll = 0x0100 - smsvdp.reg[0x08];
504490   } else {
505      xScroll = (((reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - reg[0x08]);
491      xScroll = (((smsvdp.reg[0x00] & 0x40) && (line < 16)) ? 0 : 0x0100 - smsvdp.reg[0x08]);
506492   }
507493
508494   xScrollStartColumn = (xScroll >> 3);          /* x starting column tile */
r1284r1285
516502      /* Rightmost 8 columns for SMS (or 2 columns for GG) not affected by */
517503      /* vertical scrolling when bit 7 of reg[0x00] is set */
518504      if ( IS_GAMEGEAR_VDP ) {
519         yScroll = (((reg[0x00] & 0x80) && (tileColumn > 29)) ? 0 : (reg9copy) % 224);
505         yScroll = (((smsvdp.reg[0x00] & 0x80) && (tileColumn > 29)) ? 0 : (smsvdp.reg9copy) % 224);
520506      } else {
521         yScroll = (((reg[0x00] & 0x80) && (tileColumn > 23)) ? 0 : (reg9copy) % 224);
507         yScroll = (((smsvdp.reg[0x00] & 0x80) && (tileColumn > 23)) ? 0 : (smsvdp.reg9copy) % 224);
522508      }
523509
524510      tileLine = ( ( tileColumn + xScrollStartColumn ) & 0x1F ) * 2;
r1284r1285
534520      if (vertSelected) {
535521         tileLine = 0x07 - tileLine;
536522      }
537      bitPlane0 = VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x00];
538      bitPlane1 = VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x01];
539      bitPlane2 = VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x02];
540      bitPlane3 = VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x03];
523      bitPlane0 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x00];
524      bitPlane1 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x01];
525      bitPlane2 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x02];
526      bitPlane3 = smsvdp.VRAM[((tileSelected << 5) + ((tileLine & 0x07) << 2)) + 0x03];
541527
542528      for (pixelX = 0; pixelX < 8 ; pixelX++) {
543529         UINT8 penBit0, penBit1, penBit2, penBit3;
r1284r1285
561547         pixelPlotX = (0 - (xScroll & 0x07) + (tileColumn << 3) + pixelPlotX);
562548         if (pixelPlotX >= 0 && pixelPlotX < 256) {
563549//            logerror("%x %x\n", pixelPlotX + pixelOffsetX, pixelPlotY);
564            lineBuffer[pixelPlotX] = currentPalette[penSelected];
550            lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
565551            prioritySelected[pixelPlotX] = prioritySelect | ( penSelected & 0x0F );
566552         }
567553      }
568554   }
569555
570556   /* Draw sprite layer */
571   spriteHeight = (reg[0x01] & 0x02 ? 16 : 8);
557   spriteHeight = (smsvdp.reg[0x01] & 0x02 ? 16 : 8);
572558   spriteZoom = 1;
573   if (reg[0x01] & 0x01) {
559   if (smsvdp.reg[0x01] & 0x01) {
574560      /* sprite doubling */
575561      spriteZoom = 2;
576562   }
577563   spriteBufferCount = 0;
578   for (spriteIndex = 0; (spriteIndex < 64) && (spriteTable[spriteIndex] != 0xD0 || y_pixels != 192) && (spriteBufferCount < 9); spriteIndex++) {
564   for (spriteIndex = 0; (spriteIndex < 64) && (spriteTable[spriteIndex] != 0xD0 || smsvdp.y_pixels != 192) && (spriteBufferCount < 9); spriteIndex++) {
579565      spriteY = spriteTable[spriteIndex] + 1; /* sprite y position starts at line 1 */
580566      if (spriteY > 240) {
581567         spriteY -= 256; /* wrap from top if y position is > 240 */
r1284r1285
585571            spriteBuffer[spriteBufferCount] = spriteIndex;
586572         } else {
587573            /* Too many sprites per line */
588            statusReg |= STATUS_SPROVR;
574            smsvdp.status |= STATUS_SPROVR;
589575         }
590576         spriteBufferCount++;
591577      }
r1284r1285
593579   if ( spriteBufferCount > 8 ) {
594580      spriteBufferCount = 8;
595581   }
596   memset(lineCollisionBuffer, 0, SMS_X_PIXELS);
582   memset(smsvdp.collision_buffer, 0, SMS_X_PIXELS);
597583   spriteBufferCount--;
598584   for (spriteBufferIndex = spriteBufferCount; spriteBufferIndex >= 0; spriteBufferIndex--) {
599585      spriteIndex = spriteBuffer[spriteBufferIndex];
r1284r1285
603589      }
604590
605591      spriteX = spriteTable[0x80 + (spriteIndex << 1)];
606      if (reg[0x00] & 0x08) {
592      if (smsvdp.reg[0x00] & 0x08) {
607593         spriteX -= 0x08;    /* sprite shift */
608594      }
609595      spriteTileSelected = spriteTable[0x81 + (spriteIndex << 1)];
610      if (reg[0x06] & 0x04) {
596      if (smsvdp.reg[0x06] & 0x04) {
611597         spriteTileSelected += 256; /* pattern table select */
612598      }
613      if (reg[0x01] & 0x02) {
599      if (smsvdp.reg[0x01] & 0x02) {
614600         spriteTileSelected &= 0x01FE; /* force even index */
615601      }
616602      spriteLine = ( line - spriteY ) / spriteZoom;
r1284r1285
619605         spriteTileSelected += 1;
620606      }
621607
622      bitPlane0 = VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x00];
623      bitPlane1 = VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x01];
624      bitPlane2 = VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x02];
625      bitPlane3 = VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x03];
608      bitPlane0 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x00];
609      bitPlane1 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x01];
610      bitPlane2 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x02];
611      bitPlane3 = smsvdp.VRAM[((spriteTileSelected << 5) + ((spriteLine & 0x07) << 2)) + 0x03];
626612
627613      for (pixelX = 0; pixelX < 8 ; pixelX++) {
628614         UINT8 penBit0, penBit1, penBit2, penBit3;
r1284r1285
639625            continue;
640626         }
641627
642         if (reg[0x01] & 0x01) {
628         if (smsvdp.reg[0x01] & 0x01) {
643629            /* sprite doubling is enabled */
644630            pixelPlotX = spriteX + (pixelX << 1);
645631
r1284r1285
649635            }
650636
651637            if ( ! ( prioritySelected[pixelPlotX] & PRIORITY_BIT ) ) {
652               lineBuffer[pixelPlotX] = currentPalette[penSelected];
653               lineBuffer[pixelPlotX+1] = currentPalette[penSelected];
638               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
639               lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
654640            } else {
655641               if ( prioritySelected[pixelPlotX] == PRIORITY_BIT ) {
656                  lineBuffer[pixelPlotX] = currentPalette[penSelected];
642                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
657643               }
658644               if ( prioritySelected[pixelPlotX + 1] == PRIORITY_BIT ) {
659                  lineBuffer[pixelPlotX+1] = currentPalette[penSelected];
645                  lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
660646               }
661647            }
662            if (lineCollisionBuffer[pixelPlotX] != 1) {
663               lineCollisionBuffer[pixelPlotX] = 1;
648            if (smsvdp.collision_buffer[pixelPlotX] != 1) {
649               smsvdp.collision_buffer[pixelPlotX] = 1;
664650            } else {
665651               /* sprite collision occurred */
666               statusReg |= STATUS_SPRCOL;
652               smsvdp.status |= STATUS_SPRCOL;
667653            }
668            if (lineCollisionBuffer[pixelPlotX + 1] != 1) {
669               lineCollisionBuffer[pixelPlotX + 1] = 1;
654            if (smsvdp.collision_buffer[pixelPlotX + 1] != 1) {
655               smsvdp.collision_buffer[pixelPlotX + 1] = 1;
670656            } else {
671657               /* sprite collision occurred */
672               statusReg |= STATUS_SPRCOL;
658               smsvdp.status |= STATUS_SPRCOL;
673659            }
674660         } else {
675661            pixelPlotX = spriteX + pixelX;
r1284r1285
680666            }
681667
682668            if ( ! ( prioritySelected[pixelPlotX] & PRIORITY_BIT ) ) {
683               lineBuffer[pixelPlotX] = currentPalette[penSelected];
669               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
684670            } else {
685671               if ( prioritySelected[pixelPlotX] == PRIORITY_BIT ) {
686                  lineBuffer[pixelPlotX] = currentPalette[penSelected];
672                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
687673               }
688674            }
689            if (lineCollisionBuffer[pixelPlotX] != 1) {
690               lineCollisionBuffer[pixelPlotX] = 1;
675            if (smsvdp.collision_buffer[pixelPlotX] != 1) {
676               smsvdp.collision_buffer[pixelPlotX] = 1;
691677            } else {
692678               /* sprite collision occurred */
693               statusReg |= STATUS_SPRCOL;
679               smsvdp.status |= STATUS_SPRCOL;
694680            }
695681         }
696682      }
697683   }
698684
699685   /* Fill column 0 with overscan color from reg[0x07] */
700   if (reg[0x00] & 0x20) {
701      lineBuffer[0] = currentPalette[BACKDROP_COLOR];
702      lineBuffer[1] = currentPalette[BACKDROP_COLOR];
703      lineBuffer[2] = currentPalette[BACKDROP_COLOR];
704      lineBuffer[3] = currentPalette[BACKDROP_COLOR];
705      lineBuffer[4] = currentPalette[BACKDROP_COLOR];
706      lineBuffer[5] = currentPalette[BACKDROP_COLOR];
707      lineBuffer[6] = currentPalette[BACKDROP_COLOR];
708      lineBuffer[7] = currentPalette[BACKDROP_COLOR];
686   if (smsvdp.reg[0x00] & 0x20) {
687      lineBuffer[0] = smsvdp.current_palette[BACKDROP_COLOR];
688      lineBuffer[1] = smsvdp.current_palette[BACKDROP_COLOR];
689      lineBuffer[2] = smsvdp.current_palette[BACKDROP_COLOR];
690      lineBuffer[3] = smsvdp.current_palette[BACKDROP_COLOR];
691      lineBuffer[4] = smsvdp.current_palette[BACKDROP_COLOR];
692      lineBuffer[5] = smsvdp.current_palette[BACKDROP_COLOR];
693      lineBuffer[6] = smsvdp.current_palette[BACKDROP_COLOR];
694      lineBuffer[7] = smsvdp.current_palette[BACKDROP_COLOR];
709695   }
710696}
711697
712void sms_refresh_tms9918_sprites(int *lineBuffer, int line) {
698static void sms_refresh_tms9918_sprites(int *lineBuffer, int line) {
713699   int pixelPlotX;
714700   int spriteHeight, spriteBufferCount, spriteIndex, spriteBuffer[4], spriteBufferIndex;
715701   UINT8 *spriteTable, *spritePatternTable;
716702
717703   /* Draw sprite layer */
718   spriteTable = VRAM + ( ( reg[0x05] & 0x7F ) << 7 );
719   spritePatternTable = VRAM + ( ( reg[0x06] & 0x07 ) << 11 );
704   spriteTable = smsvdp.VRAM + ( ( smsvdp.reg[0x05] & 0x7F ) << 7 );
705   spritePatternTable = smsvdp.VRAM + ( ( smsvdp.reg[0x06] & 0x07 ) << 11 );
720706   spriteHeight = 8;
721   if ( reg[0x01] & 0x02 )                         /* Check if SI is set */
707   if ( smsvdp.reg[0x01] & 0x02 )                         /* Check if SI is set */
722708      spriteHeight = spriteHeight * 2;
723   if ( reg[0x01] & 0x01 )                         /* Check if MAG is set */
709   if ( smsvdp.reg[0x01] & 0x01 )                         /* Check if MAG is set */
724710      spriteHeight = spriteHeight * 2;
725711   spriteBufferCount = 0;
726712   for ( spriteIndex = 0; (spriteIndex < 32*4 ) && ( spriteTable[spriteIndex] != 0xD0 ) && ( spriteBufferCount < 5); spriteIndex+= 4 ) {
r1284r1285
733719            spriteBuffer[spriteBufferCount] = spriteIndex;
734720         } else {
735721            /* Too many sprites per line */
736            statusReg |= STATUS_SPROVR;
722            smsvdp.status |= STATUS_SPROVR;
737723         }
738724         spriteBufferCount++;
739725      }
r1284r1285
741727   if ( spriteBufferCount > 4 ) {
742728      spriteBufferCount = 4;
743729   }
744   memset( lineCollisionBuffer, 0, SMS_X_PIXELS );
730   memset( smsvdp.collision_buffer, 0, SMS_X_PIXELS );
745731   spriteBufferCount--;
746732   for ( spriteBufferIndex = spriteBufferCount; spriteBufferIndex >= 0; spriteBufferIndex-- ) {
747733      int penSelected;
r1284r1285
763749      }
764750      spriteTileSelected = spriteTable[ spriteIndex + 2 ];
765751      spriteLine = line - spriteY;
766      if ( reg[0x01] & 0x01 ) {
752      if ( smsvdp.reg[0x01] & 0x01 ) {
767753         spriteLine >>= 1;
768754      }
769      if ( reg[0x01] & 0x02 ) {
755      if ( smsvdp.reg[0x01] & 0x02 ) {
770756         spriteTileSelected &= 0xFC;
771757         if ( spriteLine > 0x07 ) {
772758            spriteTileSelected += 1;
r1284r1285
776762      pattern = spritePatternTable[ spriteTileSelected * 8 + spriteLine ];
777763
778764      for ( pixelX = 0; pixelX < 8; pixelX++ ) {
779         if ( reg[0x01] & 0x01 ) {
765         if ( smsvdp.reg[0x01] & 0x01 ) {
780766            pixelPlotX = spriteX + pixelX * 2;
781767            if ( pixelPlotX < 0 || pixelPlotX > 255 ) {
782768               continue;
783769            }
784770            if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) ) {
785               lineBuffer[pixelPlotX] = currentPalette[penSelected];
786               if ( lineCollisionBuffer[pixelPlotX] != 1 ) {
787                  lineCollisionBuffer[pixelPlotX] = 1;
771               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
772               if ( smsvdp.collision_buffer[pixelPlotX] != 1 ) {
773                  smsvdp.collision_buffer[pixelPlotX] = 1;
788774               } else {
789                  statusReg |= STATUS_SPRCOL;
775                  smsvdp.status |= STATUS_SPRCOL;
790776               }
791               lineBuffer[pixelPlotX+1] = currentPalette[penSelected];
792               if ( lineCollisionBuffer[pixelPlotX + 1] != 1 ) {
793                  lineCollisionBuffer[pixelPlotX + 1] = 1;
777               lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
778               if ( smsvdp.collision_buffer[pixelPlotX + 1] != 1 ) {
779                  smsvdp.collision_buffer[pixelPlotX + 1] = 1;
794780               } else {
795                  statusReg |= STATUS_SPRCOL;
781                  smsvdp.status |= STATUS_SPRCOL;
796782               }
797783            }
798784         } else {
r1284r1285
801787               continue;
802788            }
803789            if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) ) {
804               lineBuffer[pixelPlotX] = currentPalette[penSelected];
805               if ( lineCollisionBuffer[pixelPlotX] != 1 ) {
806                  lineCollisionBuffer[pixelPlotX] = 1;
790               lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
791               if ( smsvdp.collision_buffer[pixelPlotX] != 1 ) {
792                  smsvdp.collision_buffer[pixelPlotX] = 1;
807793               } else {
808                  statusReg |= STATUS_SPRCOL;
794                  smsvdp.status |= STATUS_SPRCOL;
809795               }
810796            }
811797         }
812798      }
813799   
814      if ( reg[0x01] & 0x02 ) {
800      if ( smsvdp.reg[0x01] & 0x02 ) {
815801         spriteTileSelected += 2;
816802         pattern = spritePatternTable[ spriteTileSelected * 8 + spriteLine ];
817         spriteX += ( reg[0x01] & 0x01 ? 16 : 8 );
803         spriteX += ( smsvdp.reg[0x01] & 0x01 ? 16 : 8 );
818804         for ( pixelX = 0; pixelX < 8; pixelX++ ) {
819            if ( reg[0x01] & 0x01 ) {
805            if ( smsvdp.reg[0x01] & 0x01 ) {
820806               pixelPlotX = spriteX + pixelX * 2;
821807               if ( pixelPlotX < 0 || pixelPlotX > 255 ) {
822808                  continue;
823809               }
824810               if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) ) {
825                  lineBuffer[pixelPlotX] = currentPalette[penSelected];
826                  if ( lineCollisionBuffer[pixelPlotX] != 1 ) {
827                     lineCollisionBuffer[pixelPlotX] = 1;
811                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
812                  if ( smsvdp.collision_buffer[pixelPlotX] != 1 ) {
813                     smsvdp.collision_buffer[pixelPlotX] = 1;
828814                  } else {
829                     statusReg |= STATUS_SPRCOL;
815                     smsvdp.status |= STATUS_SPRCOL;
830816                  }
831                  lineBuffer[pixelPlotX+1] = currentPalette[penSelected];
832                  if ( lineCollisionBuffer[pixelPlotX + 1] != 1 ) {
833                     lineCollisionBuffer[pixelPlotX + 1] = 1;
817                  lineBuffer[pixelPlotX+1] = smsvdp.current_palette[penSelected];
818                  if ( smsvdp.collision_buffer[pixelPlotX + 1] != 1 ) {
819                     smsvdp.collision_buffer[pixelPlotX + 1] = 1;
834820                  } else {
835                     statusReg |= STATUS_SPRCOL;
821                     smsvdp.status |= STATUS_SPRCOL;
836822                  }
837823               }
838824            } else {
r1284r1285
841827                  continue;
842828               }
843829               if ( penSelected && ( pattern & ( 1 << ( 7 - pixelX ) ) ) ) {
844                  lineBuffer[pixelPlotX] = currentPalette[penSelected];
845                  if ( lineCollisionBuffer[pixelPlotX] != 1 ) {
846                     lineCollisionBuffer[pixelPlotX] = 1;
830                  lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
831                  if ( smsvdp.collision_buffer[pixelPlotX] != 1 ) {
832                     smsvdp.collision_buffer[pixelPlotX] = 1;
847833                  } else {
848                     statusReg |= STATUS_SPRCOL;
834                     smsvdp.status |= STATUS_SPRCOL;
849835                  }
850836               }
851837            }
r1284r1285
854840   }
855841}
856842
857void sms_refresh_line_mode2(int *lineBuffer, int line) {
843static void sms_refresh_line_mode2(int *lineBuffer, int line) {
858844   int tileColumn;
859845   int pixelX, pixelPlotX;
860846   UINT8 *nameTable, *colorTable, *patternTable;
861847   int patternMask, colorMask, patternOffset;
862848
863849   /* Draw background layer */
864   nameTable = VRAM + ( ( reg[0x02] & 0x0F ) << 10 ) + ( ( line >> 3 ) * 32 );
865   colorTable = VRAM + ( ( reg[0x03] & 0x80 ) << 6 );
866   colorMask = ( ( reg[0x03] & 0x7F ) << 3 ) | 0x07;
867   patternTable = VRAM + ( ( reg[0x04] & 0x04 ) << 11 );
868   patternMask = ( ( reg[0x04] & 0x03 ) << 8 ) | 0xFF;
850   nameTable = smsvdp.VRAM + ( ( smsvdp.reg[0x02] & 0x0F ) << 10 ) + ( ( line >> 3 ) * 32 );
851   colorTable = smsvdp.VRAM + ( ( smsvdp.reg[0x03] & 0x80 ) << 6 );
852   colorMask = ( ( smsvdp.reg[0x03] & 0x7F ) << 3 ) | 0x07;
853   patternTable = smsvdp.VRAM + ( ( smsvdp.reg[0x04] & 0x04 ) << 11 );
854   patternMask = ( ( smsvdp.reg[0x04] & 0x03 ) << 8 ) | 0xFF;
869855   patternOffset = ( line & 0xC0 ) << 2;
870856   for ( tileColumn = 0; tileColumn < 32; tileColumn++ ) {
871857      UINT8 pattern;
r1284r1285
886872         if ( IS_GAMEGEAR_VDP ) {
887873            penSelected |= 0x10;
888874         }
889         lineBuffer[pixelPlotX] = currentPalette[penSelected];
875         lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
890876      }
891877   }
892878
r1284r1285
894880   sms_refresh_tms9918_sprites( lineBuffer, line );
895881}
896882
897void sms_refresh_line_mode0(int *lineBuffer, int line) {
883static void sms_refresh_line_mode0(int *lineBuffer, int line) {
898884   int tileColumn;
899885   int pixelX, pixelPlotX;
900886   UINT8 *nameTable, *colorTable, *patternTable;
901887
902888   /* Draw background layer */
903   nameTable = VRAM + ( ( reg[0x02] & 0x0F ) << 10 ) + ( ( line >> 3 ) * 32 );
904   colorTable = VRAM + ( ( reg[0x03] << 6 ) & ( VRAM_SIZE - 1 ) );
905   patternTable = VRAM + ( ( reg[0x04] << 11 ) & ( VRAM_SIZE - 1 ) );
889   nameTable = smsvdp.VRAM + ( ( smsvdp.reg[0x02] & 0x0F ) << 10 ) + ( ( line >> 3 ) * 32 );
890   colorTable = smsvdp.VRAM + ( ( smsvdp.reg[0x03] << 6 ) & ( VRAM_SIZE - 1 ) );
891   patternTable = smsvdp.VRAM + ( ( smsvdp.reg[0x04] << 11 ) & ( VRAM_SIZE - 1 ) );
906892   for ( tileColumn = 0; tileColumn < 32; tileColumn++ ) {
907893      UINT8 pattern;
908894      UINT8 colors;
r1284r1285
919905            penSelected |= 0x10;
920906         }
921907         pixelPlotX = ( tileColumn << 3 ) + pixelX;
922         lineBuffer[pixelPlotX] = currentPalette[penSelected];
908         lineBuffer[pixelPlotX] = smsvdp.current_palette[penSelected];
923909      }
924910   }
925911
r1284r1285
927913   sms_refresh_tms9918_sprites( lineBuffer, line );
928914}
929915
930void sms_refresh_line( mame_bitmap *bitmap, int pixelOffsetX, int pixelPlotY, int line ) {
916static void sms_refresh_line( mame_bitmap *bitmap, int pixelOffsetX, int pixelPlotY, int line ) {
931917   int x;
932   int *blitLineBuffer = lineBuffer;
918   int *blitLineBuffer = smsvdp.line_buffer;
933919
934   if ( line >= 0 && line < sms_frame_timing[ACTIVE_DISPLAY_V] ) {
935      switch( vdp_mode ) {
920   if ( line >= 0 && line < smsvdp.sms_frame_timing[ACTIVE_DISPLAY_V] ) {
921      switch( smsvdp.vdp_mode ) {
936922      case 0:
937923         sms_refresh_line_mode0( blitLineBuffer, line );
938924         break;
r1284r1285
946932      }
947933   } else {
948934      for ( x = 0; x < 256; x++ ) {
949         blitLineBuffer[x] = currentPalette[BACKDROP_COLOR];
935         blitLineBuffer[x] = smsvdp.current_palette[BACKDROP_COLOR];
950936      }
951937   }
952938
953   if ( IS_GAMEGEAR_VDP && ggSmsMode ) {
954      int *combineLineBuffer = lineBuffer + ( ( line & 0x03 ) + 1 ) * 256;
939   if ( IS_GAMEGEAR_VDP && smsvdp.gg_sms_mode ) {
940      int *combineLineBuffer = smsvdp.line_buffer + ( ( line & 0x03 ) + 1 ) * 256;
955941      int plotX = 48;
956942
957943      /* Do horizontal scaling */
r1284r1285
1000986         pixelPlotY = TBORDER_START + NTSC_192_TBORDER_Y_PIXELS + 24 + ggLine;
1001987
1002988         /* Setup our source lines */
1003         line1 = lineBuffer + ( ( ( myLine - 3 ) & 0x03 ) + 1 ) * 256;
1004         line2 = lineBuffer + ( ( ( myLine - 2 ) & 0x03 ) + 1 ) * 256;
1005         line3 = lineBuffer + ( ( ( myLine - 1 ) & 0x03 ) + 1 ) * 256;
1006         line4 = lineBuffer + ( ( ( myLine - 0 ) & 0x03 ) + 1 ) * 256;
989         line1 = smsvdp.line_buffer + ( ( ( myLine - 3 ) & 0x03 ) + 1 ) * 256;
990         line2 = smsvdp.line_buffer + ( ( ( myLine - 2 ) & 0x03 ) + 1 ) * 256;
991         line3 = smsvdp.line_buffer + ( ( ( myLine - 1 ) & 0x03 ) + 1 ) * 256;
992         line4 = smsvdp.line_buffer + ( ( ( myLine - 0 ) & 0x03 ) + 1 ) * 256;
1007993
1008994         for( x = 0+48; x < 160+48; x++ ) {
1009995            rgb_t   c1 = Machine->pens[line1[x]];
r1284r1285
10251011   }
10261012}
10271013
1028void sms_update_palette(void) {
1014static void sms_update_palette(void) {
10291015   int i;
10301016
10311017   /* Exit if palette is has no changes */
1032   if (isCRAMDirty == 0) {
1018   if (smsvdp.cram_dirty == 0) {
10331019      return;
10341020   }
1035   isCRAMDirty = 0;
1021   smsvdp.cram_dirty = 0;
10361022
1037   if ( vdp_mode != 4 && ! IS_GAMEGEAR_VDP ) {
1023   if ( smsvdp.vdp_mode != 4 && ! IS_GAMEGEAR_VDP ) {
10381024      for( i = 0; i < 16; i++ ) {
1039         currentPalette[i] = 64+i;
1025         smsvdp.current_palette[i] = 64+i;
10401026      }
10411027      return;
10421028   }
10431029
10441030   if ( IS_GAMEGEAR_VDP ) {
1045      if ( ggSmsMode ) {
1031      if ( smsvdp.gg_sms_mode ) {
10461032         for ( i = 0; i < 32; i++ ) {
1047            currentPalette[i] = ( ( CRAM[i] & 0x30 ) << 6 ) | ( ( CRAM[i] & 0x0C ) << 4 ) | ( ( CRAM[i] & 0x03 ) << 2 );
1033            smsvdp.current_palette[i] = ( ( smsvdp.CRAM[i] & 0x30 ) << 6 ) | ( ( smsvdp.CRAM[i] & 0x0C ) << 4 ) | ( ( smsvdp.CRAM[i] & 0x03 ) << 2 );
10481034         }
10491035      } else {
10501036         for ( i = 0; i < 32; i++ ) {
1051            currentPalette[i] = ( ( CRAM[i*2+1] << 8 ) | CRAM[i*2] ) & 0x0FFF;
1037            smsvdp.current_palette[i] = ( ( smsvdp.CRAM[i*2+1] << 8 ) | smsvdp.CRAM[i*2] ) & 0x0FFF;
10521038         }
10531039      }
10541040   } else {
10551041      for ( i = 0; i < 32; i++ ) {
1056         currentPalette[i] = CRAM[i] & 0x3F;
1042         smsvdp.current_palette[i] = smsvdp.CRAM[i] & 0x3F;
10571043      }
10581044   }
10591045}
r1284r1285
10611047VIDEO_UPDATE(sms) {
10621048   int x, y;
10631049
1064   if (prevBitMapSaved) {
1050   if (smsvdp.prev_bitmap_saved) {
10651051   for (y = 0; y < Machine->screen[0].height; y++) {
10661052      for (x = 0; x < Machine->screen[0].width; x++) {
1067         *BITMAP_ADDR32(bitmap, y, x) = (*BITMAP_ADDR32(tmpbitmap, y, x) + *BITMAP_ADDR32(prevBitMap, y, x)) >> 2;
1068         logerror("%x %x %x\n", *BITMAP_ADDR32(tmpbitmap, y, x), *BITMAP_ADDR32(prevBitMap, y, x), (*BITMAP_ADDR32(tmpbitmap, y, x) + *BITMAP_ADDR32(prevBitMap, y, x)) >> 2);
1053         *BITMAP_ADDR32(bitmap, y, x) = (*BITMAP_ADDR32(tmpbitmap, y, x) + *BITMAP_ADDR32(smsvdp.prev_bitmap, y, x)) >> 2;
1054         logerror("%x %x %x\n", *BITMAP_ADDR32(tmpbitmap, y, x), *BITMAP_ADDR32(smsvdp.prev_bitmap, y, x), (*BITMAP_ADDR32(tmpbitmap, y, x) + *BITMAP_ADDR32(smsvdp.prev_bitmap, y, x)) >> 2);
10691055      }
10701056   }
10711057   } else {
10721058      copybitmap(bitmap, tmpbitmap, 0, 0, 0, 0, &Machine->screen[0].visarea, TRANSPARENCY_NONE, 0);
10731059   }
1074   if (!prevBitMapSaved) {
1075      copybitmap(prevBitMap, tmpbitmap, 0, 0, 0, 0, &Machine->screen[0].visarea, TRANSPARENCY_NONE, 0);
1076   //prevBitMapSaved = 1;
1060   if (!smsvdp.prev_bitmap_saved) {
1061      copybitmap(smsvdp.prev_bitmap, tmpbitmap, 0, 0, 0, 0, &Machine->screen[0].visarea, TRANSPARENCY_NONE, 0);
1062   //smsvdp.prev_bitmap_saved = 1;
10771063   }
10781064   return 0;
10791065}

Previous 15994 Revisions Next


© 1998-2018 The MESS Team