summaryrefslogtreecommitdiff
path: root/plugins/gme/game-music-emu-0.6pre/gme/Kss_Core.h
blob: 3c9d8d9c45dc8c351343c48826166c011c82d9d2 (plain)
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
// MSX computer KSS music file emulator

// Game_Music_Emu 0.6-pre
#ifndef KSS_CORE_H
#define KSS_CORE_H

#include "Gme_Loader.h"
#include "Rom_Data.h"
#include "Z80_Cpu.h"

class Kss_Core : public Gme_Loader {
public:
	// KSS file header
	struct header_t
	{
		enum { size = 0x20 };
		enum { base_size = 0x10 };
		enum { ext_size = size - base_size };
		
		byte tag [4];
		byte load_addr [2];
		byte load_size [2];
		byte init_addr [2];
		byte play_addr [2];
		byte first_bank;
		byte bank_mode;
		byte extra_header;
		byte device_flags;
		
		// KSSX extended data, if extra_header==0x10
		byte data_size [4];
		byte unused [4];
		byte first_track [2];
		byte last_track [2]; // if no extended data, we set this to 0xFF
		byte psg_vol;
		byte scc_vol;
		byte msx_music_vol;
		byte msx_audio_vol;
	};
	
	// Header for currently loaded file
	header_t const& header() const { return header_; }
	
	typedef int time_t;
	void set_play_period( time_t p )        { play_period = p; }
	
	blargg_err_t start_track( int );
	
	blargg_err_t end_frame( time_t );

protected:
	typedef Z80_Cpu Kss_Cpu;
	Kss_Cpu cpu;
	
	void set_bank( int logical, int physical );
	
	typedef int addr_t;
	virtual void cpu_write( addr_t, int ) = 0;
	virtual int  cpu_in(  time_t, addr_t );
	virtual void cpu_out( time_t, addr_t, int );
	
	// Called after one frame of emulation
	virtual void update_gain() = 0;
	
// Implementation
public:
	Kss_Core();
	virtual ~Kss_Core();

protected:
	virtual blargg_err_t load_( Data_Reader& );
	virtual void unload();

private:
	enum { idle_addr = 0xFFFF };
	
	Rom_Data rom;
	header_t header_;
	bool gain_updated;
	int bank_count;
	time_t play_period;
	time_t next_play;
	
	// large items
	enum { mem_size = 0x10000 };
	byte ram [mem_size + Kss_Cpu::cpu_padding];
	byte unmapped_read  [0x100]; // TODO: why isn't this page_size?
	// because CPU can't read beyond this in last page? or because it will spill into unmapped_write?
	
	byte unmapped_write [Kss_Cpu::page_size];
	
	int bank_size() const { return (16 * 1024) >> (header_.bank_mode >> 7 & 1); }
	bool run_cpu( time_t end );
	void jsr( byte const (&addr) [2] );
};

#endif