OSDN Git Service

5f12d6556e54a63d0ed7815e831f1836d9fe2c5a
[csp-qt/common_source_project-fm7.git] / source / src / vm / fmtowns / ad7820kr.h
1 /*
2         Author : Kyuma.Ohta <whatisthis.sowhat _at_ gmail.com>
3         Date   : 2019.01.29-
4
5         [ADC AD7820KR with FIFO]
6 */
7 #pragma once
8
9 /*
10   ADC: 
11     Reading data via read_signal().
12     Setting data (from external device) via write_signal().
13 */
14 #include "../device.h"
15 #include "../../common.h"
16 #include "../../fifo.h"
17
18 #define SIG_AD7820_RESET         1
19 #define SIG_AD7820_DO_SAMPLE     2  // SET A DATA (from any internal device)
20 #define SIG_AD7820_PERIOD_OUT    3  // TICK A CLOCK
21 #define SIG_AD7820_POP_FIFO      4
22 #define SIG_AD7820_PEEK_FIFO     5
23
24 // ToDo: Adjust sample rate.
25 class AD7820KR : public DEVICE {
26         // ADC
27         outputs_t output_data;
28         FIFO* in_fifo;
29         uint32_t adc_data;
30
31         int in_rate;
32         int out_rate;
33         
34         int in_mod;
35         int out_count;
36         int out_mod;
37         
38         void push_fifo(uint32_t data);
39
40 public:
41         AD7820KR(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu)
42         {
43                 adc_fifo = new FIFO(96000 * 2); // DEPTH OK?
44                 initialize_output_signals(&output_data);
45                 set_device_name(_T("AD7820KR A/D CONVERTER"));
46         }
47         ~AD7820KR()
48         {
49                 delete adc_fifo;
50         }
51
52         void initialize();
53         void release();
54         void reset();
55
56         uint32_t read_signal(int ch);
57
58         // Note: To sample:
59         // 1. Set input rate (maybe host AUDIO_INPUT) and output rate (maybe emulated devices's rate).
60         // 2. Get nSamples from HOST.
61         // 3. Convert (adjust) sampled datas width to this device (maybe signed int or float -> unsigned 8bits).
62         // 4. Push a data to this device's FIFO via write_signal(SIG_AD7820_DO_SAMPLE, converted_data) or set_data().
63         // 5. Repeat 4. nSamples times.
64         // 6. Go to 2.
65         // Note2: To get sampled data (from emulated devices):
66         // 1. Use set_context_output_data() to set target device(s).
67         // 2. Transfer data within target's write_signal().
68         // 3. Trigger to this device by AD7820KR::write_signal(SIG_AD7820_PERIOD_OUT,...).
69         // See, vm/fmtowns/adpcm.[cpp|h] .
70         void write_signal(int ch, uint32_t data, uint32_t mask);
71
72         void set_data(uint8_t data)
73         {
74                 push_fifo(data);
75         }
76         void set_data(int8_t data)
77         {
78                 uint8_t _n;
79                 if(data < 0) {
80                         _n = (uint8_t)(-data);
81                         _n = 128 - _n;
82                 } else {
83                         _n = (uint8_t)data;
84                         _n = _n + 128;
85                 }
86                 push_fifo(_n);
87         }
88         void set_data(int32_t data)
89         {
90                 uint8_t _n;
91                 data >>= 24;
92                 _n = (uint8_t)(data + 128);
93                 push_fifo(_n);
94         }
95         void set_data(uint32_t data)
96         {
97                 uint8_t _n;
98                 data >>= 24;
99                 _n = (uint8_t)data;
100                 push_fifo(_n);
101         }
102         void set_data(int16_t data)
103         {
104                 uint8_t _n;
105                 data >>= 8;
106                 _n = (uint8_t)(data + 128);
107                 push_fifo(_n);
108         }
109         void set_data(uint16_t data)
110         {
111                 uint8_t _n;
112                 data >>= 8;
113                 _n = (uint8_t)data;
114                 push_fifo(_n);
115         }
116         // Note: These functions are to set sample rate both in/out.
117         void initialize_sampler(uint32_t irate, uint32_t orate);
118         void change_in_rate(int rate);
119         void change_in_period(double us);
120         void change_out_rate(int rate);
121         void change_out_period(double us);
122         
123         bool process_state(FILEIO* state_fio, bool loading);
124
125         void set_context_output_data(DEVICE* device, int id, uint32_t mask)
126         {
127                 register_output_signal(&output_data, device, id, mask);
128         }
129
130 };
131