OSDN Git Service

00cf5331413fe25e0fa332f4fbeef4380d6ea9da
[csp-qt/common_source_project-fm7.git] / source / src / vm / fm7 / fm7_display.h
1 /*
2  * Common source code project -> FM-7 -> Display
3  * (C) 2015 K.Ohta <whatisthis.sowhat _at_ gmail.com>
4  * History:
5  *  Feb 10, 2015 : Initial.
6  */
7
8 #ifndef _CSP_FM7_DISPLAY_H
9 #define _CSP_FM7_DISPLAY_H
10
11 #include "../vm.h"
12 #include "../../common.h"
13 #include "../device.h"
14 #include "../mc6809.h"
15 #include "fm7_common.h"
16
17
18 #if defined(_FM77AV40EX) || defined(_FM77AV40SX)
19 #define __FM7_GVRAM_PAG_SIZE (0x2000 * 24)
20 #elif defined(_FM77AV40)
21 #define __FM7_GVRAM_PAG_SIZE (0x2000 * 18)
22 #elif defined(_FM77AV_VARIANTS)
23 #define __FM7_GVRAM_PAG_SIZE (0x2000 * 12)
24 #else
25 #define __FM7_GVRAM_PAG_SIZE (0x4000 * 3)
26 #endif
27
28 class DEVICE;
29 class MC6809;
30 class MB61VH010;
31 #if defined(_FM77L4)
32 class HD46505;
33 #endif
34
35 namespace FM7 {
36         class FM7_MAINIO;
37         class KEYBOARD;
38         class KANJIROM;
39 }
40
41 namespace FM7 {
42
43         
44 class DISPLAY: public DEVICE
45 {
46 private:
47         __DECL_ALIGNED(16) uint16_t bit_trans_table_0[256][8];
48         __DECL_ALIGNED(16) uint16_t bit_trans_table_1[256][8];
49         __DECL_ALIGNED(16) uint16_t bit_trans_table_2[256][8];
50         __DECL_ALIGNED(16) uint16_t bit_trans_table_3[256][8];
51 #if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
52         __DECL_ALIGNED(16) uint16_t bit_trans_table_4[256][8];
53         __DECL_ALIGNED(16) uint16_t bit_trans_table_5[256][8];
54 #endif
55
56         uint32_t yoff_d1, yoff_d2;
57         uint32_t yoff_d;
58 protected:
59         __DECL_ALIGNED(32) uint32_t (__FASTCALL DISPLAY::*read_cpu_func_table[512])(uint32_t);
60         __DECL_ALIGNED(32) uint32_t (__FASTCALL DISPLAY::*read_dma_func_table[512])(uint32_t);
61         __DECL_ALIGNED(32) void (__FASTCALL DISPLAY::*write_cpu_func_table[512])(uint32_t, uint8_t);
62         __DECL_ALIGNED(32) void (__FASTCALL DISPLAY::*write_dma_func_table[512])(uint32_t, uint8_t);
63         
64         bool delay_busy;
65         bool screen_update_flag;
66         bool crt_flag_bak;
67         
68         void go_subcpu();
69         void halt_subcpu();
70         void setup_display_mode(void);
71    
72         void __FASTCALL do_nmi(bool);
73         void __FASTCALL do_irq(bool);
74         void __FASTCALL do_firq(bool);
75         void __FASTCALL set_multimode(uint8_t val);
76         uint8_t get_multimode(void);
77         uint8_t get_cpuaccessmask(void);
78         void __FASTCALL set_dpalette(uint32_t addr, uint8_t val);
79         uint8_t __FASTCALL get_dpalette(uint32_t addr);
80         void enter_display(void);
81         void leave_display(void);
82         void halt_subsystem(void);
83         void restart_subsystem(void);
84         void set_crtflag(void);
85         void reset_crtflag(void);
86         uint8_t acknowledge_irq(void);
87         uint8_t beep(void);
88         uint8_t attention_irq(void);
89         void __FASTCALL set_cyclesteal(uint8_t val);
90         uint8_t set_vramaccess(void);
91         void reset_vramaccess(void);
92         uint8_t reset_subbusy(void);
93         void set_subbusy(void);
94         void reset_some_devices(void);
95         void reset_subcpu(bool _check_firq);
96         void setup_400linemode(uint8_t val);
97    
98 #if defined(_FM77AV_VARIANTS)
99         void __FASTCALL alu_write_cmdreg(uint32_t val);
100         void __FASTCALL alu_write_logical_color(uint8_t val);
101         void __FASTCALL alu_write_mask_reg(uint8_t val);
102         void __FASTCALL alu_write_cmpdata_reg(int addr, uint8_t val);
103         void __FASTCALL alu_write_disable_reg(uint8_t val);
104         void __FASTCALL alu_write_tilepaint_data(uint32_t addr, uint8_t val);
105         void __FASTCALL alu_write_offsetreg_hi(uint8_t val);
106         void __FASTCALL alu_write_offsetreg_lo(uint8_t val);
107         void __FASTCALL alu_write_linepattern_hi(uint8_t val);
108         void __FASTCALL alu_write_linepattern_lo(uint8_t val);
109         void __FASTCALL alu_write_line_position(int addr, uint8_t val);
110         
111         uint8_t get_miscreg(void);
112         void __FASTCALL set_miscreg(uint8_t val);
113         void __FASTCALL set_monitor_bank(uint8_t var);
114         void __FASTCALL set_apalette_index_hi(uint8_t val);
115         void __FASTCALL set_apalette_index_lo(uint8_t val);
116         void __FASTCALL set_apalette_b(uint8_t val);
117         void __FASTCALL set_apalette_r(uint8_t val);
118         void __FASTCALL set_apalette_g(uint8_t val);
119         void __FASTCALL calc_apalette(uint16_t idx);
120
121 #endif // _FM77AV_VARIANTS
122         
123         void copy_vram_all();
124         void copy_vram_per_line(int begin, int end);
125         void copy_vram_blank_area(void);
126         void __FASTCALL draw_window(int dmode, int y, int begin, int bytes, bool window_inv, bool scan_line);
127         void __FASTCALL clear_display(int dmode, int w, int h);
128  private:
129         bool sub_busy;
130         bool firq_mask;
131         bool vblank;
132         bool vsync;
133         bool hblank;
134         bool cancel_request;
135         bool key_firq_req;
136         bool clock_fast;
137         int display_mode;
138         bool halt_flag;
139         int active_page; // GVRAM is Double-Buffer.
140         uint32_t page_offset;
141         uint32_t page_mask;
142         uint32_t pagemod_mask;
143         
144         uint32_t prev_clock;
145         uint8_t frame_skip_count_draw;
146         uint8_t frame_skip_count_transfer;
147         bool need_transfer_line;
148         bool palette_changed;
149         
150         // Event handler
151         int nmi_event_id;
152
153 //#if defined(_FM77AV_VARIANTS) || defined(_FM77L4)
154         int hblank_event_id;
155         int hdisp_event_id;
156         int vsync_event_id;
157         int vstart_event_id;
158
159         int vblank_count;
160 //#endif
161 #if defined(_FM77AV_VARIANTS)
162         bool subcpu_resetreq;
163         bool power_on_reset;
164 #endif  
165         uint32_t displine;
166         
167         DEVICE *ins_led;
168         DEVICE *kana_led;
169         DEVICE *caps_led;
170         
171         int8_t display_page;
172         int8_t display_page_bak;
173 #if defined(_FM77_VARIANTS)
174 # if defined(_FM77L4)
175         bool mode400line;
176         bool stat_400linecard;
177 # endif 
178         bool kanjisub;
179         pair32_t kanjiaddr;
180 #elif defined(_FM77AV_VARIANTS)
181         bool kanjisub;
182         pair32_t kanjiaddr;
183 # if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)|| \
184      defined(_FM77AV20) || defined(_FM77AV20EX) || defined(_FM77AV20SX)
185         bool mode400line;
186         bool mode256k;
187 # endif
188         bool mode320;
189         int cgrom_bank;
190 #if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)|| \
191     defined(_FM77AV20) || defined(_FM77AV20SX) || defined(_FM77AV20EX)
192         int vram_bank;
193         bool vram_page;
194         uint8_t console_ram_bank;
195         uint8_t vram_active_block;
196         uint8_t vram_display_block;
197         
198 # if defined(_FM77AV40EX) || defined(_FM77AV40SX)
199         uint16_t window_low;
200         uint16_t window_high;
201         uint16_t window_xbegin;
202         uint16_t window_xend;
203
204         bool window_opened;
205 # endif 
206 #endif  
207         bool nmi_enable;
208         bool diag_load_subrom_a;
209         bool diag_load_subrom_b;
210         bool diag_load_subrom_cg;
211 #endif
212         bool diag_load_subrom_c;
213
214         scrntype_t dpalette_pixel[8];
215         scrntype_t dpalette_pixel_tmp[8];
216 #if defined(USE_GREEN_DISPLAY)
217         scrntype_t dpalette_pixel_green[8];
218         scrntype_t dpalette_green_tmp[8];
219         bool use_green_monitor;
220 #endif
221         
222         uint8_t dpalette_data[8];
223 #if defined(_FM77AV_VARIANTS)
224         pair32_t apalette_index;
225         uint8_t analog_palette_r[4096];
226         uint8_t analog_palette_g[4096];
227         uint8_t analog_palette_b[4096];
228         scrntype_t analog_palette_pixel[4096];
229         scrntype_t analog_palette_pixel_tmp[4096];
230 #endif // FM77AV etc...
231 #if defined(_FM77AV_VARIANTS)
232         uint8_t io_w_latch[0x40];
233 #elif !defined(_FM77AV40EX) && !defined(_FM77AV40SX)
234         uint8_t io_w_latch[0x10];
235 #else
236         uint8_t io_w_latch[0x100];
237 #endif
238         uint8_t multimode_accessmask;
239         uint8_t multimode_dispmask;
240         __DECL_ALIGNED(8) bool multimode_accessflags[4];
241         __DECL_ALIGNED(8) bool multimode_dispflags[4];
242    
243         uint32_t offset_point;
244         pair32_t tmp_offset_point[2];
245         bool offset_changed[2];
246         bool offset_77av;
247    
248 #if defined(_FM77AV_VARIANTS)
249         uint8_t subrom_bank;
250         uint8_t subrom_bank_using;
251         uint32_t offset_point_bank1;
252 # if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
253         bool monitor_ram;
254         bool ram_protect;
255 # endif
256 #endif  
257
258         // VRAM Write Page.
259         //uint8_t  write_access_page;
260         
261         // ROM/RAM on Sub-System.
262         __DECL_ALIGNED(16) uint8_t gvram[__FM7_GVRAM_PAG_SIZE];
263         __DECL_ALIGNED(16) uint8_t gvram_shadow[__FM7_GVRAM_PAG_SIZE];
264         
265         __DECL_ALIGNED(16) uint8_t console_ram[0x1000];
266         __DECL_ALIGNED(16) uint8_t work_ram[0x380];
267         __DECL_ALIGNED(16) uint8_t shared_ram[0x80];
268    
269         uint8_t subsys_c[0x2800];
270 #if defined(_FM77AV_VARIANTS)
271         uint8_t subsys_a[0x2000];
272         uint8_t subsys_b[0x2000];
273         uint8_t subsys_cg[0x2000];
274         uint8_t submem_hidden[0x300];
275 #endif
276
277         bool crt_flag;
278         bool vram_accessflag;
279         bool is_cyclesteal;
280 #if defined(_FM77L4)
281         __DECL_ALIGNED(16) uint8_t subsys_cg_l4[0x1000];
282         uint8_t subsys_l4[0x4800];
283         __DECL_ALIGNED(16) uint8_t text_vram[0x1000];
284         uint8_t crtc_regs[18];
285         uint8_t text_scroll_count;
286         bool workram_l4;
287         bool cursor_lsb;
288         
289         bool text_blink;
290         bool cursor_blink;
291         bool text_width40;
292         
293         pair32_t text_start_addr;
294         uint32_t text_lines;
295         uint32_t text_xmax;
296         uint32_t text_ymax;
297         
298         pair32_t cursor_addr;
299         int cursor_start;
300         int cursor_end;
301         uint8_t cursor_type;
302         
303         int event_id_l4_text_blink;
304         int event_id_l4_cursor_blink;
305 #endif
306         
307 #if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
308         __DECL_ALIGNED(16) uint8_t submem_cgram[0x4000];
309         __DECL_ALIGNED(16) uint8_t submem_console_av40[0x2000];
310         __DECL_ALIGNED(16) uint8_t subsys_ram[0x2000];
311         uint8_t cgram_bank;
312         bool kanji_level2;
313         FM7::KANJIROM *kanjiclass1;
314         FM7::KANJIROM *kanjiclass2;
315 #elif defined(_FM77_VARIANTS)
316         FM7::KANJIROM *kanjiclass1;
317 #endif
318         bool force_update;
319         bool vram_wrote_shadow;
320         __DECL_ALIGNED(16) bool vram_wrote_table[411 * 5];
321         __DECL_ALIGNED(16) bool vram_draw_table[411];
322         //uint8_t vram_wrote_pages[411];
323         __DECL_ALIGNED(16) uint32_t vram_wrote_addr_1[411];
324         __DECL_ALIGNED(16) uint32_t vram_wrote_addr_2[411];
325 #if defined(_FM77AV_VARIANTS)
326         bool use_alu;
327         MB61VH010 *alu;
328 #endif  
329         FM7::FM7_MAINIO *mainio;
330         FM7::KEYBOARD *keyboard;
331         bool vram_wrote;
332 #if defined(_FM77L4)
333         HD46505 *l4crtc;
334 #endif
335         MC6809 *subcpu;
336         
337         void CopyDrawnData(scrntype_t* src, scrntype_t* dst, int width, bool scan_line);
338 #if defined(_FM77L4)
339         scrntype_t GETVRAM_TEXTCOLOR(uint8_t attr, bool do_green);
340         uint8_t GETVRAM_TEXTPIX(uint8_t bitdata, bool reverse, bool cursor_rev);
341         void GETVRAM_1_400L(int yoff, scrntype_t *p);
342         void GETVRAM_1_400L_GREEN(int yoff, scrntype_t *p);
343         void cursor_blink_77l4();
344         void text_blink_77l4();
345 #endif
346         
347 #if defined(_FM77AV_VARIANTS)   
348         void GETVRAM_4096(int yoff, scrntype_t *p, scrntype_t *px, uint32_t rgbmask, bool window_inv = false, bool scan_line = false);
349 #endif
350 #if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)
351         void GETVRAM_256k(int yoff, scrntype_t *p, scrntype_t *px, bool scan_line = false);
352 #endif   
353         uint32_t __FASTCALL read_mmio(uint32_t addr);
354         
355         void init_read_table(void);
356         void init_write_table(void);
357         
358         uint32_t __FASTCALL read_vram_data8(uint32_t addr);
359         uint32_t __FASTCALL read_cpu_vram_data8(uint32_t addr);
360         uint32_t __FASTCALL read_dma_vram_data8(uint32_t addr);
361         uint32_t __FASTCALL read_console_ram(uint32_t addr);
362         uint32_t __FASTCALL read_work_ram(uint32_t addr);
363         uint32_t __FASTCALL read_shared_ram(uint32_t addr);
364 #if defined(_FM77AV_VARIANTS)
365         uint32_t __FASTCALL read_hidden_ram(uint32_t addr);
366 #endif
367         uint32_t __FASTCALL read_cgrom(uint32_t addr);
368         uint32_t __FASTCALL read_subsys_monitor(uint32_t addr);
369         
370         void __FASTCALL write_vram_data8(uint32_t addr, uint8_t data);
371         void __FASTCALL write_cpu_vram_data8(uint32_t addr, uint8_t data);
372         void __FASTCALL write_dma_vram_data8(uint32_t addr, uint8_t data);
373         void __FASTCALL write_console_ram(uint32_t addr, uint8_t data);
374         void __FASTCALL write_work_ram(uint32_t addr, uint8_t data);
375         void __FASTCALL write_shared_ram(uint32_t addr, uint8_t data);
376 #if defined(_FM77AV_VARIANTS)
377         void __FASTCALL write_hidden_ram(uint32_t addr, uint8_t data);
378 #endif
379 #if defined(_FM77AV40) || defined(_FM77AV40SX) || defined(_FM77AV40EX)
380         void __FASTCALL write_subsys_cgram(uint32_t addr, uint8_t data);
381         void __FASTCALL write_subsys_ram(uint32_t addr, uint8_t data);
382 #endif
383         void __FASTCALL write_mmio(uint32_t addr, uint8_t data);
384         void __FASTCALL write_dummy(uint32_t addr, uint8_t data);
385    
386         uint32_t read_bios(const _TCHAR *name, uint8_t *ptr, uint32_t size);
387
388         void event_callback_vstart(void);
389         void event_callback_vsync(void);
390         void event_callback_hdisp(void);
391         void event_callback_hblank(void);
392
393         template <class T>
394         void __FASTCALL call_write_signal(T *np, int id, uint32_t data, uint32_t mask)
395         {
396                 //T *nnp = static_cast<T *>(np);
397                 static_cast<T *>(np)->write_signal(id, data, mask);
398         }
399         template <class T>
400                 void __FASTCALL call_write_data8(T *np, uint32_t addr, uint32_t data)
401         {
402                 //T *nnp = static_cast<T *>(np);
403                 static_cast<T *>(np)->write_data8(addr, data);
404         }
405         template <class T>
406                 uint32_t __FASTCALL call_read_data8(T *np, uint32_t addr)
407         {
408                 //T *nnp = static_cast<T *>(np);
409                 return static_cast<T *>(np)->read_data8(addr);
410         }
411         template <class T>
412                 void __FASTCALL call_write_dma_data8(T *np, uint32_t addr, uint32_t data)
413         {
414                 //T *nnp = static_cast<T *>(np);
415                 static_cast<T *>(np)->write_dma_data8(addr, data);
416         }
417         template <class T>
418                 uint32_t __FASTCALL call_read_dma_data8(T *np, uint32_t addr)
419         {
420                 //T *nnp = static_cast<T *>(np);
421                 return static_cast<T *>(np)->read_dma_data8(addr);
422         }
423
424 #if defined(_FM77L4)
425         void __FASTCALL draw_77l4_400l(bool ff);
426 #endif
427 public:
428         DISPLAY(VM_TEMPLATE* parent_vm, EMU_TEMPLATE* parent_emu);
429         ~DISPLAY();
430         void __FASTCALL event_callback(int event_id, int err);
431         void __FASTCALL write_signal(int id, uint32_t data, uint32_t mask);
432         uint32_t __FASTCALL read_signal(int id);
433         
434         uint32_t __FASTCALL read_data8(uint32_t addr);
435         void __FASTCALL write_data8(uint32_t addr, uint32_t data);
436         
437         uint32_t __FASTCALL read_dma_data8(uint32_t addr);
438         void __FASTCALL write_dma_data8(uint32_t addr, uint32_t data);
439         
440         void initialize();
441         void release();
442         void reset();
443         void update_config();
444         
445         void draw_screen();
446         void event_frame();
447         void event_vline(int v, int clock);
448         bool process_state(FILEIO *state_fio, bool loading);
449         
450         int get_screen_mode(void) {
451                 return display_mode;
452         }
453         uint32_t __FASTCALL read_io8(uint32_t addr) { // This is only for debug.
454 #if defined(_FM77AV40EX) || defined(_FM77AV40SX)
455                 return io_w_latch[addr & 0xff];
456 #elif defined(_FM77AV_VARIANTS) // Really?
457                 return io_w_latch[addr & 0x3f];
458 #else
459                 return io_w_latch[addr & 0x0f];
460 #endif
461         }
462         bool screen_update(void);
463         void reset_screen_update(void);
464         void set_context_kanjiclass1(DEVICE *p) {
465 #if defined(_FM77_VARIANTS) || \
466     defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX) || \
467     defined(_FM77AV20) || defined(_FM77AV20EX) || defined(_FM77AV20SX)
468                 kanjiclass1 = (FM7::KANJIROM *)p;
469 #endif
470         }
471         void set_context_kanjiclass2(DEVICE *p) {
472 #if defined(_FM77AV40) || defined(_FM77AV40EX) || defined(_FM77AV40SX)|| \
473     defined(_FM77AV20) || defined(_FM77AV20EX) || defined(_FM77AV20SX)
474                 kanjiclass2 = (FM7::KANJIROM *)p;
475                 if(p != NULL) kanji_level2 = true;
476 #endif
477         }
478         void set_context_mainio(DEVICE *p) {
479                 mainio = (FM7::FM7_MAINIO *)p;
480         }
481         void set_context_keyboard(DEVICE *p) {
482                 keyboard = (FM7::KEYBOARD *)p;
483         }
484         void set_context_subcpu(DEVICE *p) {
485                 subcpu = (MC6809 *)p;
486         }
487 #if defined(_FM77AV_VARIANTS)
488         void set_context_alu(DEVICE *p) {
489                 alu = (MB61VH010 *)p;
490         }
491 #endif
492 #if defined(_FM77L4)
493         void set_context_l4crtc(HD46505 *p) {
494                 l4crtc = p;
495         }
496 #endif
497 };  
498
499 }
500
501 #endif //  _CSP_FM7_DISPLAY_H