1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
|
/***************************************************************************
* __________ __ ___.
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
* \/ \/ \/ \/ \/
* $Id$
*
* Pacbox - a Pacman Emulator for Rockbox
*
* Based on PIE - Pacman Instructional Emulator
*
* Copyright (c) 1997-2003,2004 Alessandro Scotti
* http://www.ascotti.org/
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
****************************************************************************/
#include "arcade.h"
#include "hardware.h"
#include <string.h>
#include "plugin.h"
extern const struct plugin_api* rb;
#ifndef HAVE_LCD_COLOR
/* Convert RGB888 to 2-bit greyscale - logic taken from bmp2rb.c */
static fb_data rgb_to_gray(unsigned int r, unsigned int g, unsigned int b)
{
int brightness = ( 2*r + 4*g + b );
if( r == 0 && g == 0 && b == 0 )
return 3;
brightness = (brightness/450);
if( brightness > 2 ) return 0;
else return 2-brightness;
}
#endif
unsigned char color_data_[256] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0b, 0x01,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0b, 0x03,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0b, 0x05,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0b, 0x07,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0b, 0x01, 0x09,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x0f, 0x00, 0x0e, 0x00, 0x01, 0x0c, 0x0f,
0x00, 0x0e, 0x00, 0x0b, 0x00, 0x0c, 0x0b, 0x0e,
0x00, 0x0c, 0x0f, 0x01, 0x00, 0x00, 0x00, 0x00,
0x00, 0x01, 0x02, 0x0f, 0x00, 0x07, 0x0c, 0x02,
0x00, 0x09, 0x06, 0x0f, 0x00, 0x0d, 0x0c, 0x0f,
0x00, 0x05, 0x03, 0x09, 0x00, 0x0f, 0x0b, 0x00,
0x00, 0x0e, 0x00, 0x0b, 0x00, 0x0e, 0x00, 0x0b,
0x00, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x0e, 0x01,
0x00, 0x0f, 0x0b, 0x0e, 0x00, 0x0e, 0x00, 0x0f,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
unsigned char palette_data_[0x20] = {
0x00, 0x07, 0x66, 0xef, 0x00, 0xf8, 0xea, 0x6f,
0x00, 0x3f, 0x00, 0xc9, 0x38, 0xaa, 0xaf, 0xf6,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
enum {
Normal = 0x00,
FlipY = 0x01,
FlipX = 0x02,
FlipXY = 0x03
};
/* Putting this in IRAM actually slows down the iPods, but is good for
the Coldfire
*/
#ifdef CPU_COLDFIRE
fb_data palette[256] IBSS_ATTR; /* Color palette */
#else
fb_data palette[256]; /* Color palette */
#endif
void init_PacmanMachine(int dip)
{
int i;
/* Initialize the CPU and the RAM */
z80_reset();
rb->memset( &ram_[0x4000], 0xFF, 0x1000 );
/* Initialize parameters */
port1_ = 0xFF;
port2_ = 0xFF;
coin_counter_ = 0;
/* Reset the machine */
reset_PacmanMachine();
/* Set the DIP switches to a default configuration */
setDipSwitches( dip );
/* Initialize the video character translation tables: video memory has a
very peculiar arrangement in Pacman so we precompute a few tables to
move around faster */
for( i=0x000; i<0x400; i++ ) {
int x, y;
if( i < 0x040 ) {
x = 29 - (i & 0x1F);
y = 34 + (i >> 5);
}
else if( i >= 0x3C0 ) {
x = 29 - (i & 0x1F);
y = ((i-0x3C0) >> 5);
}
else {
x = 27 - ((i-0x40) >> 5);
y = 2 + ((i-0x40) & 0x1F);
}
if( (y >= 0) && (y < 36) && (x >= 0) && (x < 28) )
vchar_to_i_[i] = y*28 + x;
else
vchar_to_i_[i] = 0x3FF;
}
}
void reset_PacmanMachine(void)
{
int i;
z80_reset();
output_devices_ = 0;
interrupt_vector_ = 0;
rb->memset( ram_+0x4000, 0, 0x1000 );
rb->memset( color_mem_, 0, sizeof(color_mem_) );
rb->memset( video_mem_, 0, sizeof(video_mem_) );
rb->memset( dirty_, 0, sizeof(dirty_) );
for( i=0; i<8; i++ ) {
sprites_[i].color = 0;
sprites_[i].x = ScreenWidth;
}
}
/*
Run the machine for one frame.
*/
int run(void)
{
/* Run until the CPU has executed the number of cycles per frame
(the function returns the number of "extra" cycles spent by the
last instruction but that is not really important here) */
unsigned extraCycles = z80_run( CpuCyclesPerFrame );
/* Reset the CPU cycle counter to make sure it doesn't overflow,
also take into account the extra cycles from the previous run */
setCycles( extraCycles );
/* If interrupts are enabled, force a CPU interrupt with the vector
set by the program */
if( output_devices_ & InterruptEnabled ) {
z80_interrupt( interrupt_vector_ );
}
return 0;
}
/** Returns the status of the coin lockout door. */
unsigned char getCoinLockout(void) {
return output_devices_ & CoinLockout ? 1 : 0;
}
static void decodeCharByte( unsigned char b, unsigned char * charbuf, int charx, int chary, int charwidth )
{
int i;
for( i=3; i>=0; i-- ) {
charbuf[charx+(chary+i)*charwidth] = (b & 1) | ((b >> 3) & 2);
b >>= 1;
}
}
static void decodeCharLine( unsigned char * src, unsigned char * charbuf, int charx, int chary, int charwidth )
{
int x;
for( x=7; x>=0; x-- ) {
decodeCharByte( *src++, charbuf, x+charx, chary, charwidth );
}
}
static void decodeCharSet( unsigned char * mem, unsigned char * charset )
{
int i;
for( i=0; i<256; i++ ) {
unsigned char * src = mem + 16*i;
unsigned char * dst = charset + 64*i;
decodeCharLine( src, dst, 0, 4, 8 );
decodeCharLine( src+8, dst, 0, 0, 8 );
}
}
static void decodeSprites( unsigned char * mem, unsigned char * sprite_data )
{
int i;
for( i=0; i<64; i++ ) {
unsigned char * src = mem + i*64;
unsigned char * dst = sprite_data + 256*i;
decodeCharLine( src , dst, 8, 12, 16 );
decodeCharLine( src+ 8, dst, 8, 0, 16 );
decodeCharLine( src+16, dst, 8, 4, 16 );
decodeCharLine( src+24, dst, 8, 8, 16 );
decodeCharLine( src+32, dst, 0, 12, 16 );
decodeCharLine( src+40, dst, 0, 0, 16 );
decodeCharLine( src+48, dst, 0, 4, 16 );
decodeCharLine( src+56, dst, 0, 8, 16 );
}
}
/*
Decode one byte from the encoded color palette.
An encoded palette byte contains RGB information bit-packed as follows:
bit: 7 6 5 4 3 2 1 0
color: b b g g g r r r
*/
static unsigned decodePaletteByte( unsigned char value )
{
unsigned bit0, bit1, bit2;
unsigned red, green, blue;
bit0 = (value >> 0) & 0x01;
bit1 = (value >> 1) & 0x01;
bit2 = (value >> 2) & 0x01;
red = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
bit0 = (value >> 3) & 0x01;
bit1 = (value >> 4) & 0x01;
bit2 = (value >> 5) & 0x01;
green = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
bit0 = 0;
bit1 = (value >> 6) & 0x01;
bit2 = (value >> 7) & 0x01;
blue = 0x21 * bit0 + 0x47 * bit1 + 0x97 * bit2;
return (blue << 16 ) | (green << 8) | red;
}
void decodeROMs(void)
{
unsigned decoded_palette[0x20];
unsigned c;
int i;
decodeCharSet( charset_rom_, charmap_ );
decodeSprites( spriteset_rom_, spritemap_ );
for( i=0x00; i<0x20; i++ ) {
decoded_palette[i] = decodePaletteByte( palette_data_[i] );
}
for( i=0; i<256; i++ ) {
c = decoded_palette[ color_data_[i] & 0x0F ];
#ifdef HAVE_LCD_COLOR
palette[i] = LCD_RGBPACK((unsigned char) (c),
(unsigned char) (c >> 8),
(unsigned char) (c >> 16));
#else
palette[i] = rgb_to_gray((unsigned char) (c),
(unsigned char) (c >> 8),
(unsigned char) (c >> 16) );
#endif
}
}
void getDeviceInfo( enum InputDevice device, unsigned char * mask, unsigned char ** port )
{
static unsigned char MaskInfo[] = {
0x01 , // Joy1_Up
0x02 , // Joy1_Left
0x04 , // Joy1_Right
0x08 , // Joy1_Down
0x10 , // Switch_RackAdvance
0x20 , // CoinSlot_1
0x40 , // CoinSlot_2
0x80 , // Switch_AddCredit
0x01 , // Joy2_Up
0x02 , // Joy2_Left
0x04 , // Joy2_Right
0x08 , // Joy2_Down
0x10 , // Switch_Test
0x20 , // Key_OnePlayer
0x40 , // Key_TwoPlayers
0x80 // Switch_CocktailMode
};
*mask = MaskInfo[device];
switch( device ) {
case Joy1_Up:
case Joy1_Left:
case Joy1_Right:
case Joy1_Down:
case Switch_RackAdvance:
case CoinSlot_1:
case CoinSlot_2:
case Switch_AddCredit:
*port = &port1_;
break;
case Joy2_Up:
case Joy2_Left:
case Joy2_Right:
case Joy2_Down:
case Switch_Test:
case Key_OnePlayer:
case Key_TwoPlayers:
case Switch_CocktailMode:
*port = &port2_;
break;
default:
*port = 0;
break;
}
}
enum InputDeviceMode getDeviceMode( enum InputDevice device )
{
unsigned char mask;
unsigned char * port;
getDeviceInfo( device, &mask, &port );
return (*port & mask) == 0 ? DeviceOn : DeviceOff;
}
/*
Fire an input event, telling the emulator for example
that the joystick has been released from the down position.
*/
void setDeviceMode( enum InputDevice device, enum InputDeviceMode mode )
{
if( (getCoinLockout() == 0) && ((device == CoinSlot_1)||(device == CoinSlot_2)||(device == Switch_AddCredit)) ) {
// Coin slots are locked, ignore command and exit
return;
}
unsigned char mask;
unsigned char * port;
getDeviceInfo( device, &mask, &port );
if( mode == DeviceOn )
*port &= ~mask;
else if( mode == DeviceOff )
*port |= mask;
else if( mode == DeviceToggle )
*port ^= mask;
}
void setDipSwitches( unsigned value ) {
dip_switches_ = (unsigned char) value;
setDeviceMode( Switch_RackAdvance, value & DipRackAdvance_Auto ? DeviceOn : DeviceOff );
setDeviceMode( Switch_Test, value & DipMode_Test ? DeviceOn : DeviceOff );
setDeviceMode( Switch_CocktailMode, value & DipCabinet_Cocktail ? DeviceOn : DeviceOff );
}
unsigned getDipSwitches(void) {
unsigned result = dip_switches_;
if( getDeviceMode(Switch_RackAdvance) == DeviceOn ) result |= DipRackAdvance_Auto;
if( getDeviceMode(Switch_Test) == DeviceOn ) result |= DipMode_Test;
if( getDeviceMode(Switch_CocktailMode) == DeviceOn ) result |= DipCabinet_Cocktail;
return result;
}
#if defined (CPU_COLDFIRE)
extern void drawChar( unsigned char * buffer, int index, int ox, int oy, int color );
#else
static inline void drawChar( unsigned char * buffer, int index, int ox, int oy, int color )
{
int x,y;
/* Make the index point to the character offset into the character table */
unsigned char * chrmap = charmap_ + index*64;
buffer += ox + oy*224; /* Make the buffer point to the character position*/
color = (color & 0x3F)*4;
if( color == 0 )
{
for( y=7; y>=0; y-- )
{
rb->memset( buffer, 0, 8 );
buffer += ScreenWidth;
};
return;
};
if( output_devices_ & FlipScreen ) {
// Flip character
buffer += 7*ScreenWidth;
for( y=7; y>=0; y-- ) {
for( x=7; x>=0; x-- ) {
*buffer++ = (*chrmap++) + color;
}
buffer -= ScreenWidth + 8; // Go to the next line
}
}
else {
for( y=7; y>=0; y-- ) {
for( x=7; x>=0; x-- ) {
*buffer++ = (*chrmap++) + color;
}
buffer += ScreenWidth - 8; // Go to the next line
}
}
}
#endif
inline void drawSprite( unsigned char * buffer, int index )
{
struct PacmanSprite ps = sprites_[index];
int x,y;
char * s, * s2;
// Exit now if sprite not visible at all
if( (ps.color == 0) || (ps.x >= ScreenWidth) || (ps.y < 16) || (ps.y >= (ScreenHeight-32)) ) {
return;
}
// Clip the sprite coordinates to cut the parts that fall off the screen
int start_x = (ps.x < 0) ? 0 : ps.x;
int end_x = (ps.x < (ScreenWidth-16)) ? ps.x+15 : ScreenWidth-1;
// Prepare variables for drawing
int color = (ps.color & 0x3F)*4;
unsigned char * spritemap_base = spritemap_ + ((ps.n & 0x3F)*256);
buffer += ScreenWidth*ps.y;
dirty_[(start_x >> 3) + (ps.y >> 3)*28] = 1;
dirty_[(start_x >> 3) + 1 + (ps.y >> 3)*28] = 1;
dirty_[(end_x >> 3) + (ps.y >> 3)*28] = 1;
dirty_[(start_x >> 3) + ((ps.y >> 3)+1)*28] = 1;
dirty_[(start_x >> 3) + 1 + ((ps.y >> 3)+1)*28] = 1;
dirty_[(end_x >> 3) + ((ps.y >> 3)+1)*28] = 1;
dirty_[(start_x >> 3) + ((ps.y+15) >> 3)*28] = 1;
dirty_[(start_x >> 3) + 1 + ((ps.y+15) >> 3)*28] = 1;
dirty_[(end_x >> 3) + ((ps.y+15) >> 3)*28] = 1;
// Draw the 16x16 sprite
if( ps.mode == 0 ) { // Normal
s2 = spritemap_base + start_x-ps.x;
// Draw the 16x16 sprite
for( y=15; y>=0; y-- ) {
s = s2;
for( x=start_x; x<=end_x; x++, s++ ) {
if( *s ) {
buffer[x] = color + *s;
}
}
buffer += ScreenWidth;
s2 += 16;
}
} else if( ps.mode == 1 ) { // Flip Y
s2 = spritemap_base + start_x-ps.x + 240;
for( y=15; y>=0; y-- ) {
s = s2;
for( x=start_x; x<=end_x; x++, s++ ) {
if( *s ) {
buffer[x] = color + *s;
}
}
buffer += ScreenWidth;
s2 -= 16;
}
} else if( ps.mode == 2 ) { // Flip X
s2 = spritemap_base + 15 + ps.x-start_x;
for( y=15; y>=-0; y-- ) {
s = s2;
for( x=start_x; x<=end_x; x++, s-- ) {
if( *s ) {
buffer[x] = color + *s;
}
}
buffer += ScreenWidth;
s2 += 16;
}
} else { // Flip X and Y
s2 = spritemap_base + 255 + ps.x-start_x;
for( y=15; y>=0; y-- ) {
s = s2;
for( x=start_x; x<=end_x; x++, s-- ) {
if( *s ) {
buffer[x] = color + *s;
}
}
buffer += ScreenWidth;
s2 -= 16;
}
}
}
/*
Draw the video into the specified buffer.
*/
bool renderBackground( unsigned char * buffer )
{
unsigned char * video = video_mem_;
unsigned char * color = color_mem_;
unsigned char * dirty = dirty_;
int x,y;
bool changed=false;
// Draw the background first...
if( output_devices_ & FlipScreen ) {
for( y=ScreenHeight-CharHeight; y>=0; y-=CharHeight ) {
for( x=ScreenWidth-CharWidth; x>=0; x-=CharWidth ) {
if (*dirty) {
drawChar( buffer, *video++, x, y, *color++ );
*(dirty++)=0;
changed=true;
} else {
dirty++;
video++;
color++;
}
}
}
}
else {
for( y=0; y<ScreenHeight; y+=CharHeight ) {
for( x=0; x<ScreenWidth; x+=CharWidth ) {
if (*dirty) {
drawChar( buffer, *video++, x, y, *color++ );
*(dirty++)=0;
changed=true;
} else {
dirty++;
video++;
color++;
}
}
}
}
return changed;
}
void renderSprites( unsigned char * buffer )
{
int i;
// ...then add the sprites
for( i=7; i>=0; i-- ) {
drawSprite( buffer, i );
}
}
/* Enables/disables the speed hack. */
int setSpeedHack( int enabled )
{
int result = 0;
if( enabled ) {
if( (ram_[0x180B] == 0xBE) && (ram_[0x1FFD] == 0x00) ) {
// Patch the ROM to activate the speed hack
ram_[0x180B] = 0x01; // Activate speed hack
ram_[0x1FFD] = 0xBD; // Fix ROM checksum
result = 1;
}
}
else {
if( (ram_[0x180B] == 0x01) && (ram_[0x1FFD] == 0xBD) ) {
// Restore the patched ROM locations
ram_[0x180B] = 0xBE;
ram_[0x1FFD] = 0x00;
result = 1;
}
}
return result;
}
|