OSDN Git Service

[VM][STATE] Use namespace {VMNAME} to separate per VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / pcengine / pce.h
1 /*
2         NEC-HE PC Engine Emulator 'ePCEngine'
3         SHARP X1twin Emulator 'eX1twin'
4
5         Origin : Ootake (joypad/cdrom)
6                : xpce (psg)
7                : MESS (vdc/vce/vpc/cdrom)
8         Author : Takeda.Toshiya
9         Date   : 2009.03.11-
10
11         [ PC-Eninge ]
12 */
13
14 #ifndef _PCE_H_
15 #define _PCE_H_
16
17 #include "../vm.h"
18 #include "../../emu.h"
19 #include "../device.h"
20
21 #ifdef SUPPORT_CDROM
22 #define SIG_PCE_SCSI_IRQ        0
23 #define SIG_PCE_SCSI_DRQ        1
24 #define SIG_PCE_SCSI_BSY        2
25 #define SIG_PCE_CDDA_DONE       3
26 #define SIG_PCE_ADPCM_VCLK      4
27 #endif
28
29 #define VDC_WPF         684     /* width of a line in frame including blanking areas */
30 #define VDC_LPF         262     /* number of lines in a single frame */
31 #ifdef SUPPORT_CDROM
32 #define ADPCM_CLOCK     9216000
33 #endif
34
35 #define USE_CPU_HUC6280
36
37 class HUC6280;
38 #ifdef SUPPORT_CDROM
39 class MSM5205;
40 class SCSI_HOST;
41 class SCSI_CDROM;
42 #endif
43
44 namespace PCEDEV {
45
46 class PCE : public DEVICE
47 {
48 private:
49         HUC6280* d_cpu;
50 #ifdef SUPPORT_CDROM
51         MSM5205* d_msm;
52         SCSI_HOST* d_scsi_host;
53         SCSI_CDROM* d_scsi_cdrom;
54 #endif
55         
56         bool support_6btn_pad;
57         bool support_multi_tap;
58 #ifdef SUPPORT_SUPER_GFX
59         bool support_sgfx;
60 #endif
61 #ifdef SUPPORT_CDROM
62         bool support_cdrom;
63 #endif
64         
65         // memory
66         uint8_t cart[0x400000]; // max 4mb
67 #ifdef SUPPORT_SUPER_GFX
68         uint8_t ram[0x8000];    // ram 32kb
69 #else
70         uint8_t ram[0x2000];    // ram 8kb
71 #endif
72 #ifdef SUPPORT_BACKUP_RAM
73         uint8_t backup[0x2000];
74         uint32_t backup_crc32;
75 #endif
76         uint32_t bank;
77         uint8_t buffer;
78         int prev_width;
79         bool inserted;
80         
81         // vdc
82         struct {
83                 int dvssr_write;                /* Set when the DVSSR register has been written to */
84                 int physical_width;             /* Width of the display */
85                 int physical_height;            /* Height of the display */
86                 uint16_t sprite_ram[64*4];      /* Sprite RAM */
87                 int curline;                    /* the current scanline we're on */
88                 int current_segment;            /* current segment of display */
89                 int current_segment_line;       /* current line inside a segment of display */
90                 int vblank_triggered;           /* to indicate whether vblank has been triggered */
91                 int raster_count;               /* counter to compare RCR against */
92                 int satb_countdown;             /* scanlines to wait to trigger the SATB irq */
93                 uint8_t vram[0x10000];
94                 uint8_t inc;
95                 uint8_t vdc_register;
96                 uint8_t vdc_latch;
97                 pair_t vdc_data[32];
98                 int status;
99                 int y_scroll;
100         } vdc[2];
101         
102         struct {
103                 uint8_t vce_control;            /* VCE control register */
104                 pair_t vce_address;             /* Current address in the palette */
105                 pair_t vce_data[512];           /* Palette data */
106                 int current_bitmap_line;        /* The current line in the display we are on */
107                 //bitmap_ind16 *bmp;
108                 scrntype_t bmp[VDC_LPF][VDC_WPF];
109                 scrntype_t palette[1024];
110         } vce;
111         
112         struct {
113                 struct {
114                         UINT8 prio;
115                         UINT8 vdc0_enabled;
116                         UINT8 vdc1_enabled;
117                 } vpc_prio[4];
118                 UINT8   prio_map[512];          /* Pre-calculated priority map */
119                 pair_t  priority;                       /* Priority settings registers */
120                 pair_t  window1;                        /* Window 1 setting */
121                 pair_t  window2;                        /* Window 2 setting */
122                 UINT8   vdc_select;                     /* Which VDC do the ST0, ST1, and ST2 instructions write to */
123         } vpc;
124         
125         void pce_interrupt();
126 #ifdef SUPPORT_SUPER_GFX
127         void sgx_interrupt();
128 #endif
129         void vdc_reset();
130         void vdc_advance_line(int which);
131         void draw_black_line(int line);
132         void draw_overscan_line(int line);
133 #ifdef SUPPORT_SUPER_GFX
134         void draw_sgx_overscan_line(int line);
135 #endif
136         void vram_write(int which, uint32_t offset, uint8_t data);
137         uint8_t vram_read(int which, uint32_t offset);
138         void vdc_w(int which, uint16_t offset, uint8_t data);
139         uint8_t vdc_r(int which, uint16_t offset);
140         void vce_w(uint16_t offset, uint8_t data);
141         uint8_t vce_r(uint16_t offset);
142         void pce_refresh_line(int which, int line, int external_input, uint8_t *drawn, scrntype_t *line_buffer);
143         void conv_obj(int which, int i, int l, int hf, int vf, char *buf);
144         void pce_refresh_sprites(int which, int line, uint8_t *drawn, scrntype_t *line_buffer);
145         void vdc_do_dma(int which);
146         void vpc_update_prio_map();
147         void vpc_w(uint16_t offset, uint8_t data);
148         uint8_t vpc_r(uint16_t offset);
149 #ifdef SUPPORT_SUPER_GFX
150         void sgx_vdc_w(uint16_t offset, uint8_t data);
151         uint8_t sgx_vdc_r(uint16_t offset);
152 #endif
153         
154         // psg
155         typedef struct {
156                 // registers
157                 uint8_t regs[8];
158                 uint8_t wav[32];
159                 uint8_t wavptr;
160                 // sound gen
161                 uint32_t genptr;
162                 uint32_t remain;
163                 bool noise;
164                 uint32_t randval;
165         } psg_t;
166         psg_t psg[8];
167         uint8_t psg_ch, psg_vol, psg_lfo_freq, psg_lfo_ctrl;
168         int sample_rate;
169         int volume_l, volume_r;
170         void psg_reset();
171         void psg_write(uint16_t addr, uint8_t data);
172         uint8_t psg_read(uint16_t addr);
173         
174         // joypad
175         const uint32_t *joy_stat;
176         uint8_t joy_counter;
177         bool joy_high_nibble, joy_second_byte;
178         
179         void joy_reset();
180         void joy_write(uint16_t addr, uint8_t data);
181         uint8_t joy_read(uint16_t addr);
182         uint8_t joy_2btn_pad_r(uint8_t index);
183         uint8_t joy_6btn_pad_r(uint8_t index);
184         
185 #ifdef SUPPORT_CDROM
186         // cd-rom
187         uint8_t cdrom_ram[0x40000];
188         uint8_t cdrom_regs[16];
189         bool backup_locked;
190         bool irq_status, drq_status;
191         
192         void cdrom_initialize();
193         void cdrom_reset();
194         void cdrom_write(uint16_t addr, uint8_t data);
195         uint8_t cdrom_read(uint16_t addr);
196         void write_cdrom_data(uint8_t data);
197         uint8_t read_cdrom_data();
198         void set_ack();
199         void clear_ack();
200         void set_cdrom_irq_line(int num, int state);
201         
202         uint8_t adpcm_ram[0x10000];
203         int adpcm_read_ptr, adpcm_write_ptr, adpcm_written;
204         int adpcm_length, adpcm_clock_divider;
205         uint8_t adpcm_read_buf, adpcm_write_buf;
206         bool adpcm_dma_enabled;
207         int msm_start_addr, msm_end_addr, msm_half_addr;
208         uint8_t msm_nibble, msm_idle;
209         
210         void reset_adpcm();
211         void write_adpcm_ram(uint8_t data);
212         uint8_t read_adpcm_ram();
213         void adpcm_do_dma();
214         void adpcm_play();
215         void adpcm_stop();
216         
217         double cdda_volume, adpcm_volume;
218         int event_cdda_fader, event_adpcm_fader;
219         
220         void cdda_fade_in(int time);
221         void cdda_fade_out(int time);
222         void adpcm_fade_in(int time);
223         void adpcm_fade_out(int time);
224 #endif
225         
226 public:
227         PCE(VM_TEMPLATE* parent_vm, EMU* parent_emu) : DEVICE(parent_vm, parent_emu)
228         {
229                 volume_l = volume_r = 1024;
230                 set_device_name(_T("PC-Engine Core"));
231         }
232         ~PCE() {}
233         
234         // common functions
235         void initialize();
236         void release();
237         void reset();
238         void event_vline(int v, int clock);
239         void write_data8(uint32_t addr, uint32_t data);
240         uint32_t read_data8(uint32_t addr);
241         void write_io8(uint32_t addr, uint32_t data);
242         uint32_t read_io8(uint32_t addr);
243 #ifdef SUPPORT_CDROM
244         void write_signal(int id, uint32_t data, uint32_t mask);
245         void event_callback(int event_id, int err);
246 #endif
247         void mix(int32_t* buffer, int cnt);
248         void set_volume(int ch, int decibel_l, int decibel_r);
249         bool process_state(FILEIO* state_fio, bool loading);
250         
251         // unique functions
252         void set_context_cpu(HUC6280* device)
253         {
254                 d_cpu = device;
255         }
256 #ifdef SUPPORT_CDROM
257         void set_context_adpcm(MSM5205* device)
258         {
259                 d_msm = device;
260         }
261         void set_context_scsi_host(SCSI_HOST* device)
262         {
263                 d_scsi_host = device;
264         }
265         void set_context_scsi_cdrom(SCSI_CDROM* device)
266         {
267                 d_scsi_cdrom = device;
268         }
269 #endif
270         void initialize_sound(int rate)
271         {
272                 sample_rate = rate;
273         }
274         void open_cart(const _TCHAR* file_path);
275         void close_cart();
276         bool is_cart_inserted()
277         {
278                 return inserted;
279         }
280         void draw_screen();
281 };
282
283 }
284 #endif