OSDN Git Service

[VM][STATE] Apply new framework to some VMs.
[csp-qt/common_source_project-fm7.git] / source / src / vm / mc6847.cpp
1 /*
2         Skelton for retropc emulator
3
4         Author : Takeda.Toshiya
5         Date   : 2010.08.03-
6
7         [ mc6847 ]
8 */
9
10 #include "vm.h"
11 #include "emu.h"
12 #include "mc6847.h"
13
14 #ifndef MC6847_VRAM_OFS
15 #define MC6847_VRAM_OFS 0
16 #endif
17
18 #define LIGHTGREEN      0
19 #define YELLOW          1
20 #define BLUE            2
21 #define RED             3
22 #define WHITE           4
23 #define CYAN            5
24 #define MAGENTA         6
25 #define ORANGE          7
26 #define BLACK           8
27 // text
28 #define GREEN           9
29 #define BEIGE           10
30 // phc20
31 #define GRAY            11
32
33 // from mess m6847.c
34 static const uint8_t intfont[64 * 12] = {
35         0x00, 0x00, 0x38, 0x44, 0x04, 0x34, 0x4C, 0x4C, 0x38, 0x00, 0x00, 0x00,
36         0x00, 0x00, 0x10, 0x28, 0x44, 0x44, 0x7C, 0x44, 0x44, 0x00, 0x00, 0x00,
37         0x00, 0x00, 0x78, 0x24, 0x24, 0x38, 0x24, 0x24, 0x78, 0x00, 0x00, 0x00,
38         0x00, 0x00, 0x38, 0x44, 0x40, 0x40, 0x40, 0x44, 0x38, 0x00, 0x00, 0x00,
39         0x00, 0x00, 0x78, 0x24, 0x24, 0x24, 0x24, 0x24, 0x78, 0x00, 0x00, 0x00,
40         0x00, 0x00, 0x7C, 0x40, 0x40, 0x70, 0x40, 0x40, 0x7C, 0x00, 0x00, 0x00,
41         0x00, 0x00, 0x7C, 0x40, 0x40, 0x70, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00,
42         0x00, 0x00, 0x38, 0x44, 0x40, 0x40, 0x4C, 0x44, 0x38, 0x00, 0x00, 0x00,
43         0x00, 0x00, 0x44, 0x44, 0x44, 0x7C, 0x44, 0x44, 0x44, 0x00, 0x00, 0x00,
44         0x00, 0x00, 0x38, 0x10, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00, 0x00, 0x00,
45         0x00, 0x00, 0x04, 0x04, 0x04, 0x04, 0x04, 0x44, 0x38, 0x00, 0x00, 0x00,
46         0x00, 0x00, 0x44, 0x48, 0x50, 0x60, 0x50, 0x48, 0x44, 0x00, 0x00, 0x00,
47         0x00, 0x00, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x7C, 0x00, 0x00, 0x00,
48         0x00, 0x00, 0x44, 0x6C, 0x54, 0x54, 0x44, 0x44, 0x44, 0x00, 0x00, 0x00,
49         0x00, 0x00, 0x44, 0x44, 0x64, 0x54, 0x4C, 0x44, 0x44, 0x00, 0x00, 0x00,
50         0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00,
51         0x00, 0x00, 0x78, 0x44, 0x44, 0x78, 0x40, 0x40, 0x40, 0x00, 0x00, 0x00,
52         0x00, 0x00, 0x38, 0x44, 0x44, 0x44, 0x54, 0x48, 0x34, 0x00, 0x00, 0x00,
53         0x00, 0x00, 0x78, 0x44, 0x44, 0x78, 0x50, 0x48, 0x44, 0x00, 0x00, 0x00,
54         0x00, 0x00, 0x38, 0x44, 0x40, 0x38, 0x04, 0x44, 0x38, 0x00, 0x00, 0x00,
55         0x00, 0x00, 0x7C, 0x10, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00,
56         0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00,
57         0x00, 0x00, 0x44, 0x44, 0x44, 0x28, 0x28, 0x10, 0x10, 0x00, 0x00, 0x00,
58         0x00, 0x00, 0x44, 0x44, 0x44, 0x44, 0x54, 0x6C, 0x44, 0x00, 0x00, 0x00,
59         0x00, 0x00, 0x44, 0x44, 0x28, 0x10, 0x28, 0x44, 0x44, 0x00, 0x00, 0x00,
60         0x00, 0x00, 0x44, 0x44, 0x28, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00,
61         0x00, 0x00, 0x7C, 0x04, 0x08, 0x10, 0x20, 0x40, 0x7C, 0x00, 0x00, 0x00,
62         0x00, 0x00, 0x38, 0x20, 0x20, 0x20, 0x20, 0x20, 0x38, 0x00, 0x00, 0x00,
63         0x00, 0x00, 0x00, 0x40, 0x20, 0x10, 0x08, 0x04, 0x00, 0x00, 0x00, 0x00,
64         0x00, 0x00, 0x38, 0x08, 0x08, 0x08, 0x08, 0x08, 0x38, 0x00, 0x00, 0x00,
65         0x00, 0x00, 0x10, 0x38, 0x54, 0x10, 0x10, 0x10, 0x10, 0x00, 0x00, 0x00,
66         0x00, 0x00, 0x00, 0x10, 0x20, 0x7C, 0x20, 0x10, 0x00, 0x00, 0x00, 0x00,
67         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
68         0x00, 0x00, 0x10, 0x10, 0x10, 0x10, 0x10, 0x00, 0x10, 0x00, 0x00, 0x00,
69         0x00, 0x00, 0x28, 0x28, 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70         0x00, 0x00, 0x28, 0x28, 0x7C, 0x28, 0x7C, 0x28, 0x28, 0x00, 0x00, 0x00,
71         0x00, 0x00, 0x10, 0x3C, 0x50, 0x38, 0x14, 0x78, 0x10, 0x00, 0x00, 0x00,
72         0x00, 0x00, 0x60, 0x64, 0x08, 0x10, 0x20, 0x4C, 0x0C, 0x00, 0x00, 0x00,
73         0x00, 0x00, 0x20, 0x50, 0x50, 0x20, 0x54, 0x48, 0x34, 0x00, 0x00, 0x00,
74         0x00, 0x00, 0x10, 0x10, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75         0x00, 0x00, 0x08, 0x10, 0x20, 0x20, 0x20, 0x10, 0x08, 0x00, 0x00, 0x00,
76         0x00, 0x00, 0x20, 0x10, 0x08, 0x08, 0x08, 0x10, 0x20, 0x00, 0x00, 0x00,
77         0x00, 0x00, 0x00, 0x10, 0x54, 0x38, 0x38, 0x54, 0x10, 0x00, 0x00, 0x00,
78         0x00, 0x00, 0x00, 0x10, 0x10, 0x7C, 0x10, 0x10, 0x00, 0x00, 0x00, 0x00,
79         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x20, 0x40, 0x00, 0x00,
80         0x00, 0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
81         0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00,
82         0x00, 0x00, 0x00, 0x04, 0x08, 0x10, 0x20, 0x40, 0x00, 0x00, 0x00, 0x00,
83         0x00, 0x00, 0x38, 0x44, 0x4C, 0x54, 0x64, 0x44, 0x38, 0x00, 0x00, 0x00,
84         0x00, 0x00, 0x10, 0x30, 0x10, 0x10, 0x10, 0x10, 0x38, 0x00, 0x00, 0x00,
85         0x00, 0x00, 0x38, 0x44, 0x04, 0x38, 0x40, 0x40, 0x7C, 0x00, 0x00, 0x00,
86         0x00, 0x00, 0x38, 0x44, 0x04, 0x08, 0x04, 0x44, 0x38, 0x00, 0x00, 0x00,
87         0x00, 0x00, 0x08, 0x18, 0x28, 0x48, 0x7C, 0x08, 0x08, 0x00, 0x00, 0x00,
88         0x00, 0x00, 0x7C, 0x40, 0x78, 0x04, 0x04, 0x44, 0x38, 0x00, 0x00, 0x00,
89         0x00, 0x00, 0x38, 0x40, 0x40, 0x78, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00,
90         0x00, 0x00, 0x7C, 0x04, 0x08, 0x10, 0x20, 0x40, 0x40, 0x00, 0x00, 0x00,
91         0x00, 0x00, 0x38, 0x44, 0x44, 0x38, 0x44, 0x44, 0x38, 0x00, 0x00, 0x00,
92         0x00, 0x00, 0x38, 0x44, 0x44, 0x3C, 0x04, 0x04, 0x38, 0x00, 0x00, 0x00,
93         0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x00,
94         0x00, 0x00, 0x00, 0x00, 0x10, 0x00, 0x00, 0x10, 0x10, 0x20, 0x00, 0x00,
95         0x00, 0x00, 0x08, 0x10, 0x20, 0x40, 0x20, 0x10, 0x08, 0x00, 0x00, 0x00,
96         0x00, 0x00, 0x00, 0x00, 0x7C, 0x00, 0x7C, 0x00, 0x00, 0x00, 0x00, 0x00,
97         0x00, 0x00, 0x20, 0x10, 0x08, 0x04, 0x08, 0x10, 0x20, 0x00, 0x00, 0x00,
98         0x00, 0x00, 0x38, 0x44, 0x04, 0x08, 0x10, 0x00, 0x10, 0x00, 0x00, 0x00
99 };
100
101 void MC6847::initialize()
102 {
103         MC6847_BASE::initialize();
104         
105         // semigraphics pattern
106         for(int i = 0; i < 16; i++) {
107                 for(int j = 0; j < 6; j++) {
108                         sg4[i * 12 + j] = ((i & 0x08) ? 0xf0 : 0) | ((i & 0x04) ? 0x0f : 0);
109                 }
110                 for(int j = 6; j < 12; j++) {
111                         sg4[i * 12 + j] = ((i & 0x02) ? 0xf0 : 0) | ((i & 0x01) ? 0x0f : 0);
112                 }
113         }
114         for(int i = 0; i < 64; i++) {
115                 for(int j = 0; j < 4; j++) {
116                         sg6[i * 12 + j] = ((i & 0x20) ? 0xf0 : 0) | ((i & 0x10) ? 0x0f : 0);
117                 }
118                 for(int j = 4; j < 8; j++) {
119                         sg6[i * 12 + j] = ((i & 0x08) ? 0xf0 : 0) | ((i & 0x04) ? 0x0f : 0);
120                 }
121                 for(int j = 8; j < 12; j++) {
122                         sg6[i * 12 + j] = ((i & 0x02) ? 0xf0 : 0) | ((i & 0x01) ? 0x0f : 0);
123                 }
124         }
125         // pc pallete
126         palette_pc[LIGHTGREEN] = RGB_COLOR(184,255,181);
127         palette_pc[RED       ] = RGB_COLOR(254, 65,105);
128         palette_pc[YELLOW    ] = RGB_COLOR(252,253,153);
129         palette_pc[BLUE      ] = RGB_COLOR(116, 41,255);
130         palette_pc[WHITE     ] = RGB_COLOR(241,229,232);
131         palette_pc[CYAN      ] = RGB_COLOR(124,210,213);
132         palette_pc[MAGENTA   ] = RGB_COLOR(254,113,255);
133         palette_pc[ORANGE    ] = RGB_COLOR(254,112, 35);
134         palette_pc[BLACK     ] = RGB_COLOR(  0,  0,  0);
135         palette_pc[GREEN     ] = RGB_COLOR( 22,134, 10);
136         palette_pc[BEIGE     ] = RGB_COLOR(255,198,170);
137         palette_pc[GRAY      ] = RGB_COLOR( 32, 32, 32);
138         
139         disabled = false;
140         
141         // register event
142         register_vline_event(this);
143         update_timing(CPU_CLOCKS, FRAMES_PER_SEC, LINES_PER_FRAME);
144 }
145
146 void MC6847::draw_alpha()
147 {
148         
149         int ofs = 0;
150         
151         for(int y = 0; y < 192; y += 12) {
152                 for(int x = 0; x < 256; x += 8) {
153                         uint8_t data = vram_ptr[ofs + MC6847_VRAM_OFS];
154 #ifdef MC6847_ATTR_OFS
155                         uint8_t attr = vram_ptr[ofs + MC6847_ATTR_OFS];
156 #endif
157                         if(++ofs >= vram_size) {
158                                 ofs = 0;
159                         }
160                         // vram data bits may be connected to mode signals
161                         bool as2 = as;
162                         bool intext2 = intext;
163                         bool css2 = css;
164                         bool inv2 = inv;
165 #ifdef MC6847_VRAM_AS
166                         as2 = ((data & MC6847_VRAM_AS) != 0);
167 #endif
168 #ifdef MC6847_VRAM_INTEXT
169                         intext2 = ((data & MC6847_VRAM_INTEXT) != 0);
170 #endif
171 #ifdef MC6847_VRAM_CSS
172                         css2 = ((data & MC6847_VRAM_CSS) != 0);
173 #endif
174 #ifdef MC6847_VRAM_INV
175                         inv2 = ((data & MC6847_VRAM_INV) != 0);
176 #endif
177 #ifdef MC6847_ATTR_OFS
178 #ifdef MC6847_ATTR_AS
179                         as2 = ((attr & MC6847_ATTR_AS) != 0);
180 #endif
181 #ifdef MC6847_ATTR_INTEXT
182                         intext2 = ((attr & MC6847_ATTR_INTEXT) != 0);
183 #endif
184 #ifdef MC6847_ATTR_CSS
185                         css2 = ((attr & MC6847_ATTR_CSS) != 0);
186 #endif
187 #ifdef MC6847_ATTR_INV
188                         inv2 = ((attr & MC6847_ATTR_INV) != 0);
189 #endif
190 #endif
191                         uint8_t *pattern;
192                         uint8_t col_fore, col_back;
193                         if(!as2) {
194                                 if(intext2) {
195                                         // external alphanumerics
196                                         pattern = &extfont[16 * data];
197                                 } else {
198                                         // internal alphanumerics
199                                         pattern = (uint8_t *)(&intfont[12 * (data & 0x3f)]);
200                                 }
201                                 // note: we need to overwrite the color table by each driver
202                                 static const uint8_t color_table[6] = {
203 #ifdef _PHC20
204                                         WHITE, GRAY, WHITE, GRAY, WHITE, GRAY
205 #else
206                                         LIGHTGREEN, GREEN, BEIGE, RED, GREEN, BLACK
207 #endif
208                                 };
209                                 int col = (css2 ? 2 : 0) | (inv2 ? 1 : 0);
210                                 col_fore = color_table[col];
211                                 col_back = color_table[col ^ 1];
212                         } else {
213                                 if(intext2) {
214                                         // semiggraphics 6
215                                         pattern = &sg6[12 * (data & 0x3f)];
216                                         col_fore = (css2 ? 4 : 0) | ((data >> 6) & 3);
217                                 } else {
218                                         // semiggraphics 4
219                                         pattern = &sg4[12 * (data & 0x0f)];
220                                         col_fore = (data >> 4) & 7;
221                                 }
222                                 col_back = BLACK;
223                         }
224                         for(int l = 0; l < 12; l++) {
225                                 uint8_t pat = pattern[l];
226                                 uint8_t* dest = &screen[y + l][x];
227                                 
228                                 dest[0] = (pat & 0x80) ? col_fore : col_back;
229                                 dest[1] = (pat & 0x40) ? col_fore : col_back;
230                                 dest[2] = (pat & 0x20) ? col_fore : col_back;
231                                 dest[3] = (pat & 0x10) ? col_fore : col_back;
232                                 dest[4] = (pat & 0x08) ? col_fore : col_back;
233                                 dest[5] = (pat & 0x04) ? col_fore : col_back;
234                                 dest[6] = (pat & 0x02) ? col_fore : col_back;
235                                 dest[7] = (pat & 0x01) ? col_fore : col_back;
236                         }
237                 }
238         }
239 }