summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libgme/z80_cpu.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libgme/z80_cpu.c')
-rw-r--r--lib/rbcodec/codecs/libgme/z80_cpu.c85
1 files changed, 85 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libgme/z80_cpu.c b/lib/rbcodec/codecs/libgme/z80_cpu.c
new file mode 100644
index 0000000000..a31236020e
--- /dev/null
+++ b/lib/rbcodec/codecs/libgme/z80_cpu.c
@@ -0,0 +1,85 @@
1// Game_Music_Emu 0.6-pre. http://www.slack.net/~ant/
2
3#include "z80_cpu.h"
4
5/* Copyright (C) 2006-2008 Shay Green. This module is free software; you
6can redistribute it and/or modify it under the terms of the GNU Lesser
7General Public License as published by the Free Software Foundation; either
8version 2.1 of the License, or (at your option) any later version. This
9module is distributed in the hope that it will be useful, but WITHOUT ANY
10WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12details. You should have received a copy of the GNU Lesser General Public
13License along with this module; if not, write to the Free Software Foundation,
14Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */
15
16#include "blargg_source.h"
17
18// flags, named with hex value for clarity
19int const S80 = 0x80;
20int const Z40 = 0x40;
21int const F20 = 0x20;
22int const H10 = 0x10;
23int const F08 = 0x08;
24int const V04 = 0x04;
25int const P04 = 0x04;
26int const N02 = 0x02;
27int const C01 = 0x01;
28
29void Z80_init( struct Z80_Cpu* this )
30{
31 this->cpu_state = &this->cpu_state_;
32
33 int i;
34 for ( i = 0x100; --i >= 0; )
35 {
36 int p, even = 1;
37 for ( p = i; p; p >>= 1 )
38 even ^= p;
39 int n = (i & (S80 | F20 | F08)) | ((even & 1) * P04);
40 this->szpc [i] = n;
41 this->szpc [i + 0x100] = n | C01;
42 }
43 this->szpc [0x000] |= Z40;
44 this->szpc [0x100] |= Z40;
45}
46
47static inline void set_page( struct Z80_Cpu* this, int i, void* write, void const* read )
48{
49 int offset = Z80_CPU_OFFSET( i * page_size );
50 byte * write2 = STATIC_CAST(byte *,write) - offset;
51 byte const* read2 = STATIC_CAST(byte const*,read ) - offset;
52 this->cpu_state_.write [i] = write2;
53 this->cpu_state_.read [i] = read2;
54 this->cpu_state->write [i] = write2;
55 this->cpu_state->read [i] = read2;
56}
57
58void Z80_reset( struct Z80_Cpu* this, void* unmapped_write, void const* unmapped_read )
59{
60 check( this->cpu_state == &this->cpu_state_ );
61 this->cpu_state = &this->cpu_state_;
62 this->cpu_state_.time = 0;
63 this->cpu_state_.base = 0;
64 this->end_time_ = 0;
65
66 int i;
67 for ( i = 0; i < page_count + 1; i++ )
68 set_page( this, i, unmapped_write, unmapped_read );
69
70 memset( &this->r, 0, sizeof this->r );
71}
72
73void Z80_map_mem( struct Z80_Cpu* this, addr_t start, int size, void* write, void const* read )
74{
75 // address range must begin and end on page boundaries
76 require( start % page_size == 0 );
77 require( size % page_size == 0 );
78 require( start + size <= 0x10000 );
79
80 int offset;
81 for ( offset = 0; offset < size; offset += page_size )
82 set_page( this, (start + offset) >> page_bits,
83 STATIC_CAST(char *,write) + offset,
84 STATIC_CAST(char const*,read ) + offset );
85}