Move the PCM/audio playback part of SDL into the target tree.
[kugel-rb.git] / apps / plugins / pacbox / hardware.c
blobf5a7184475ff166f3c54cadff515778577e2f082
1 /***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id$
10 * Pacbox - a Pacman Emulator for Rockbox
12 * Based on PIE - Pacman Instructional Emulator
14 * Copyright (c) 1997-2003,2004 Alessandro Scotti
15 * http://www.ascotti.org/
17 * This program is free software; you can redistribute it and/or
18 * modify it under the terms of the GNU General Public License
19 * as published by the Free Software Foundation; either version 2
20 * of the License, or (at your option) any later version.
22 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
23 * KIND, either express or implied.
25 ****************************************************************************/
27 #include "plugin.h"
28 #include "hardware.h"
30 /* The main data for Pacman */
32 unsigned char ram_[20*1024] IBSS_ATTR; // ROM (16K) and RAM (4K)
33 unsigned char charset_rom_[4*1024] IBSS_ATTR; // Character set ROM (4K)
34 unsigned char spriteset_rom_[4*1024] IBSS_ATTR; // Sprite set ROM (4K)
35 unsigned char dirty_[1024] IBSS_ATTR;
36 unsigned char video_mem_[1024] IBSS_ATTR; // Video memory (1K)
37 unsigned char color_mem_[1024] IBSS_ATTR; // Color memory (1K)
38 unsigned char charmap_[256*8*8] IBSS_ATTR; /* Character data for 256 8x8 characters */
39 unsigned char spritemap_[64*16*16]; /* Sprite data for 64 16x16 sprites */
40 unsigned char output_devices_ IBSS_ATTR; /* Output flip-flops set by the game program */
41 unsigned char interrupt_vector_ IBSS_ATTR;
42 unsigned coin_counter_ IBSS_ATTR;
43 unsigned char port1_ IBSS_ATTR;
44 unsigned char port2_ IBSS_ATTR;
45 unsigned char dip_switches_ IBSS_ATTR;
47 /* Internal tables and structures for faster access to data */
48 struct PacmanSprite sprites_[8]; /* Sprites */
49 short vchar_to_i_[1024];
53 For Z80 Environment: write a byte to memory.
55 void writeByte( unsigned addr, unsigned char b )
57 addr &= 0x7FFF;
59 if( addr < 0x4000 ) {
60 // This is a ROM address, do not write into it!
62 else if( addr < 0x4400 ) {
63 // Video memory
64 if (ram_[addr] != b) {
65 int x = vchar_to_i_[addr-0x4000];
66 ram_[addr] = b;
67 dirty_[x] = 1;
68 video_mem_[x] = b;
71 else if( addr < 0x4800 ) {
72 // Color memory
73 if (ram_[addr] != b) {
74 int x = vchar_to_i_[addr-0x4400];
75 ram_[addr] = b;
76 dirty_[x] = 1;
77 color_mem_[x] = b;
80 else if( addr < 0x4FF0 ) {
81 // Standard memory
82 ram_[addr] = b;
84 else if( addr < 0x5000 ) {
85 // Sprites
86 ram_[addr] = b;
88 unsigned idx = (addr - 0x4FF0) / 2;
90 if( addr & 1 ) {
91 sprites_[ idx ].color = b;
93 else {
94 sprites_[ idx ].n = b >> 2;
95 sprites_[ idx ].mode = b & 0x03;
98 else {
99 // Memory mapped ports
100 switch( addr ) {
101 case 0x5000:
102 // Interrupt enable
103 setOutputFlipFlop( InterruptEnabled, b & 0x01 );
104 break;
105 case 0x5001:
106 // Sound enable
107 setOutputFlipFlop( SoundEnabled, b & 0x01 );
108 break;
109 case 0x5002:
110 // Aux board enable?
111 break;
112 case 0x5003:
113 // Flip screen
114 setOutputFlipFlop( FlipScreen, b & 0x01 );
115 break;
116 case 0x5004:
117 // Player 1 start light
118 setOutputFlipFlop( PlayerOneLight, b & 0x01 );
119 break;
120 case 0x5005:
121 // Player 2 start light
122 setOutputFlipFlop( PlayerTwoLight, b & 0x01 );
123 break;
124 case 0x5006:
125 // Coin lockout: bit 0 is used to enable/disable the coin insert slots
126 // (0=disable).
127 // The coin slot is enabled at startup and (temporarily) disabled when
128 // the maximum number of credits (99) is inserted.
129 setOutputFlipFlop( CoinLockout, b & 0x01 );
130 break;
131 case 0x5007:
132 // Coin meter (coin counter incremented on 0/1 edge)
133 if( (output_devices_ & CoinMeter) == 0 && (b & 0x01) != 0 )
134 coin_counter_++;
135 setOutputFlipFlop( CoinMeter, b & 0x01 );
136 break;
137 case 0x50c0:
138 // Watchdog reset
139 break;
140 default:
141 if( addr >= 0x5040 && addr < 0x5060 ) {
142 // Sound registers
143 //SOUND sound_chip_.setRegister( addr-0x5040, b );
145 else if( addr >= 0x5060 && addr < 0x5070 ) {
146 // Sprite coordinates, x/y pairs for 8 sprites
147 unsigned idx = (addr-0x5060) / 2;
149 if( addr & 1 ) {
150 sprites_[ idx ].y = 272 - b + 1;
152 else {
153 sprites_[ idx ].x = 240 - b - 1;
155 if( idx <= 2 ) {
156 // In Pacman the first few sprites must be further offset
157 // to the left to get a correct display (is this a hack?)
158 sprites_[ idx ].x -= 1;
162 break;
168 For Z80 Environment: read from a port.
170 Note: all ports in Pacman are memory mapped so they are read with readByte().
172 unsigned char readPort( unsigned port )
174 (void)port;
175 return 0;
179 For Z80 Environment: write to a port.
181 void writePort( unsigned addr, unsigned char b )
183 if( addr == 0 ) {
184 // Sets the interrupt vector for the next CPU interrupt
185 interrupt_vector_ = b;
189 void setOutputFlipFlop( unsigned char bit, unsigned char value )
191 if( value ) {
192 output_devices_ |= bit;
194 else {
195 output_devices_ &= ~bit;