1 /*
2  *  SPDX-License-Identifier: GPL-2.0-or-later
3  *
4  *  Copyright (C) 2021-2021  The DOSBox Staging Team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License along
17  *  with this program; if not, write to the Free Software Foundation, Inc.,
18  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 #ifndef DOSBOX_INNOVATION_H
22 #define DOSBOX_INNOVATION_H
23 
24 #include "dosbox.h"
25 
26 #include <atomic>
27 #include <memory>
28 #include <mutex>
29 #include <string>
30 #include <thread>
31 #include <vector>
32 
33 #include "mixer.h"
34 #include "inout.h"
35 #include "rwqueue.h"
36 #include "../libs/residfp/SID.h"
37 
38 class Innovation {
39 public:
Innovation()40 	Innovation() : keep_rendering(false) {}
41 	void Open(const std::string &model_choice,
42 	          const std::string &clock_choice,
43 	          int filter_strength_6581,
44 	          int filter_strength_8580,
45 	          int port_choice);
46 
47 	void Close();
~Innovation()48 	~Innovation() { Close(); }
49 
50 private:
51 	void Render();
52 	uint16_t GetRemainingSamples();
53 	void MixerCallBack(uint16_t requested_samples);
54 	uint8_t ReadFromPort(io_port_t port, io_width_t width);
55 	void WriteToPort(io_port_t port, io_val_t value, io_width_t width);
56 
57 	// Managed objects
58 	mixer_channel_t channel = nullptr;
59 
60 	IO_ReadHandleObject read_handler = {};
61 	IO_WriteHandleObject write_handler = {};
62 
63 	std::vector<int16_t> play_buffer = {};
64 	static constexpr auto num_buffers = 4;
65 	RWQueue<std::vector<int16_t>> playable{num_buffers};
66 	RWQueue<std::vector<int16_t>> backstock{num_buffers};
67 	std::thread renderer = {};
68 	std::mutex service_mutex = {};
69 	std::unique_ptr<reSIDfp::SID> service = {};
70 	std::atomic_bool keep_rendering = {};
71 
72 	// Scalar members
73 	io_port_t base_port = 0;
74 	double chip_clock = 0;
75 	double sid_sample_rate = 0;
76 	size_t last_used = 0;
77 	uint16_t play_buffer_pos = 0;
78 	bool is_open = false;
79 };
80 
81 #endif
82