1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 /*
24  * Based on OPL emulation code of DOSBox
25  * Copyright (C) 2002-2009  The DOSBox Team
26  * Licensed under GPLv2+
27  * http://www.dosbox.com
28  */
29 
30 #ifndef AUDIO_SOFTSYNTH_OPL_DOSBOX_H
31 #define AUDIO_SOFTSYNTH_OPL_DOSBOX_H
32 
33 #ifndef DISABLE_DOSBOX_OPL
34 
35 #include "audio/fmopl.h"
36 
37 namespace OPL {
38 namespace DOSBox {
39 
40 struct Timer {
41 	double startTime;
42 	double delay;
43 	bool enabled, overflow, masked;
44 	uint8 counter;
45 
46 	Timer();
47 
48 	//Call update before making any further changes
49 	void update(double time);
50 
51 	//On a reset make sure the start is in sync with the next cycle
52 	void reset(double time);
53 
54 	void stop();
55 
56 	void start(double time, int scale);
57 };
58 
59 struct Chip {
60 	//Last selected register
61 	Timer timer[2];
62 	//Check for it being a write to the timer
63 	bool write(uint32 addr, uint8 val);
64 	//Read the current timer state, will use current double
65 	uint8 read();
66 };
67 
68 namespace DBOPL {
69 struct Chip;
70 } // end of namespace DBOPL
71 
72 class OPL : public ::OPL::EmulatedOPL {
73 private:
74 	Config::OplType _type;
75 	uint _rate;
76 
77 	DBOPL::Chip *_emulator;
78 	Chip _chip[2];
79 	union {
80 		uint16 normal;
81 		uint8 dual[2];
82 	} _reg;
83 
84 	void free();
85 	void dualWrite(uint8 index, uint8 reg, uint8 val);
86 public:
87 	OPL(Config::OplType type);
88 	~OPL();
89 
90 	bool init();
91 	void reset();
92 
93 	void write(int a, int v);
94 	byte read(int a);
95 
96 	void writeReg(int r, int v);
97 
isStereo()98 	bool isStereo() const { return _type != Config::kOpl2; }
99 
100 protected:
101 	void generateSamples(int16 *buffer, int length);
102 };
103 
104 } // End of namespace DOSBox
105 } // End of namespace OPL
106 
107 #endif // !DISABLE_DOSBOX_OPL
108 
109 #endif
110