PCem

view src/vid_pcjr.c @ 141:7734b12d55f0

Fixed PCjr 640x200x2 mode.
author TomW
date Sun Aug 03 20:38:04 2014 +0100
parents b53e148867e5
children
line source
1 #include <stdlib.h>
2 #include <math.h>
3 #include "ibm.h"
4 #include "device.h"
5 #include "io.h"
6 #include "mem.h"
7 #include "timer.h"
8 #include "video.h"
9 #include "vid_pcjr.h"
11 static int i_filt[8],q_filt[8];
13 typedef struct pcjr_t
14 {
15 mem_mapping_t mapping;
17 uint8_t crtc[32];
18 int crtcreg;
20 int array_index;
21 uint8_t array[32];
22 int array_ff;
23 int memctrl;//=-1;
24 uint8_t stat;
25 int addr_mode;
27 uint8_t *vram, *b8000;
29 int linepos, displine;
30 int sc, vc;
31 int dispon;
32 int con, coff, cursoron, blink;
33 int vsynctime, vadj;
34 uint16_t ma, maback;
36 int dispontime, dispofftime, vidtime;
37 int firstline, lastline;
38 } pcjr_t;
40 static uint8_t crtcmask[32] =
41 {
42 0xff, 0xff, 0xff, 0xff, 0x7f, 0x1f, 0x7f, 0x7f, 0xf3, 0x1f, 0x7f, 0x1f, 0x3f, 0xff, 0x3f, 0xff,
43 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
44 };
46 void pcjr_recalcaddress(pcjr_t *pcjr);
47 void pcjr_recalctimings(pcjr_t *pcjr);
49 void pcjr_out(uint16_t addr, uint8_t val, void *p)
50 {
51 pcjr_t *pcjr = (pcjr_t *)p;
52 uint8_t old;
53 // pclog("pcjr OUT %04X %02X\n",addr,val);
54 switch (addr)
55 {
56 case 0x3d4:
57 pcjr->crtcreg = val & 0x1f;
58 return;
59 case 0x3d5:
60 // pclog("CRTC write %02X %02x\n", pcjr->crtcreg, val);
61 old = pcjr->crtc[pcjr->crtcreg];
62 pcjr->crtc[pcjr->crtcreg] = val & crtcmask[pcjr->crtcreg];
63 if (old != val)
64 {
65 if (pcjr->crtcreg < 0xe || pcjr->crtcreg > 0x10)
66 {
67 fullchange = changeframecount;
68 pcjr_recalctimings(pcjr);
69 }
70 }
71 return;
72 case 0x3da:
73 // pclog("Array write %02X %02X\n", pcjr->array_index, val);
74 if (!pcjr->array_ff)
75 pcjr->array_index = val & 0x1f;
76 else
77 {
78 if (pcjr->array_index & 0x10)
79 val &= 0x0f;
80 pcjr->array[pcjr->array_index & 0x1f] = val;
81 }
82 pcjr->array_ff = !pcjr->array_ff;
83 break;
84 case 0x3df:
85 pcjr->memctrl = val;
86 pcjr->addr_mode = val >> 6;
87 pcjr_recalcaddress(pcjr);
88 break;
89 }
90 }
92 uint8_t pcjr_in(uint16_t addr, void *p)
93 {
94 pcjr_t *pcjr = (pcjr_t *)p;
95 // if (addr!=0x3DA) pclog("pcjr IN %04X\n",addr);
96 switch (addr)
97 {
98 case 0x3d4:
99 return pcjr->crtcreg;
100 case 0x3d5:
101 return pcjr->crtc[pcjr->crtcreg];
102 case 0x3da:
103 pcjr->array_ff = 0;
104 pcjr->stat ^= 0x10;
105 return pcjr->stat;
106 }
107 return 0xFF;
108 }
110 void pcjr_recalcaddress(pcjr_t *pcjr)
111 {
112 if ((pcjr->memctrl & 0xc0) == 0xc0)
113 {
114 pcjr->vram = &ram[(pcjr->memctrl & 0x06) << 14];
115 pcjr->b8000 = &ram[(pcjr->memctrl & 0x30) << 11];
116 // printf("VRAM at %05X B8000 at %05X\n",((pcjr->memctrl&0x6)<<14)+pcjr->base,((pcjr->memctrl&0x30)<<11)+pcjr->base);
117 }
118 else
119 {
120 pcjr->vram = &ram[(pcjr->memctrl & 0x07) << 14];
121 pcjr->b8000 = &ram[(pcjr->memctrl & 0x38) << 11];
122 // printf("VRAM at %05X B8000 at %05X\n",((pcjr->memctrl&0x7)<<14)+pcjr->base,((pcjr->memctrl&0x38)<<11)+pcjr->base);
123 }
124 }
126 void pcjr_write(uint32_t addr, uint8_t val, void *p)
127 {
128 pcjr_t *pcjr = (pcjr_t *)p;
129 if (pcjr->memctrl == -1)
130 return;
132 egawrites++;
133 // pclog("pcjr VRAM write %05X %02X %04X:%04X %04X:%04X\n",addr,val,CS,pc,DS,SI);
134 pcjr->b8000[addr & 0x3fff] = val;
135 }
137 uint8_t pcjr_read(uint32_t addr, void *p)
138 {
139 pcjr_t *pcjr = (pcjr_t *)p;
140 if (pcjr->memctrl == -1)
141 return 0xff;
143 egareads++;
144 // pclog("pcjr VRAM read %05X %02X %04X:%04X\n",addr,pcjr->b8000[addr&0x7FFF],CS,pc);
145 return pcjr->b8000[addr & 0x3fff];
146 }
148 void pcjr_recalctimings(pcjr_t *pcjr)
149 {
150 double _dispontime, _dispofftime, disptime;
151 if (pcjr->array[0] & 1)
152 {
153 disptime = pcjr->crtc[0] + 1;
154 _dispontime = pcjr->crtc[1];
155 }
156 else
157 {
158 disptime = (pcjr->crtc[0] + 1) << 1;
159 _dispontime = pcjr->crtc[1] << 1;
160 }
161 _dispofftime = disptime - _dispontime;
162 _dispontime *= CGACONST;
163 _dispofftime *= CGACONST;
164 pcjr->dispontime = (int)(_dispontime * (1 << TIMER_SHIFT));
165 pcjr->dispofftime = (int)(_dispofftime * (1 << TIMER_SHIFT));
166 }
169 static int ntsc_col[8][8]=
170 {
171 {0,0,0,0,0,0,0,0}, /*Black*/
172 {0,0,1,1,1,1,0,0}, /*Blue*/
173 {1,0,0,0,0,1,1,1}, /*Green*/
174 {0,0,0,0,1,1,1,1}, /*Cyan*/
175 {1,1,1,1,0,0,0,0}, /*Red*/
176 {0,1,1,1,1,0,0,0}, /*Magenta*/
177 {1,1,0,0,0,0,1,1}, /*Yellow*/
178 {1,1,1,1,1,1,1,1} /*White*/
179 };
181 /*static int cga4pal[8][4]=
182 {
183 {0,2,4,6},{0,3,5,7},{0,3,4,7},{0,3,4,7},
184 {0,10,12,14},{0,11,13,15},{0,11,12,15},{0,11,12,15}
185 };*/
187 void pcjr_poll(void *p)
188 {
189 // int *cgapal=cga4pal[((pcjr->col&0x10)>>2)|((cgamode&4)>>1)|((cgacol&0x20)>>5)];
190 pcjr_t *pcjr = (pcjr_t *)p;
191 uint16_t ca = (pcjr->crtc[15] | (pcjr->crtc[14] << 8)) & 0x3fff;
192 int drawcursor;
193 int x, c;
194 int oldvc;
195 uint8_t chr, attr;
196 uint16_t dat;
197 int cols[4];
198 int col;
199 int oldsc;
200 int y_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0}, y_val, y_tot;
201 int i_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0}, i_val, i_tot;
202 int q_buf[8] = {0, 0, 0, 0, 0, 0, 0, 0}, q_val, q_tot;
203 int r, g, b;
204 if (!pcjr->linepos)
205 {
206 // cgapal[0]=pcjr->col&15;
207 // printf("Firstline %i Lastline %i pcjr->displine %i\n",firstline,lastline,pcjr->displine);
208 pcjr->vidtime += pcjr->dispofftime;
209 pcjr->stat &= ~1;
210 pcjr->linepos = 1;
211 oldsc = pcjr->sc;
212 if ((pcjr->crtc[8] & 3) == 3)
213 pcjr->sc = (pcjr->sc << 1) & 7;
214 if (pcjr->dispon)
215 {
216 uint16_t offset = 0;
217 uint16_t mask = 0x1fff;
219 if (pcjr->displine < pcjr->firstline)
220 pcjr->firstline = pcjr->displine;
221 pcjr->lastline = pcjr->displine;
222 cols[0] = (pcjr->array[2] & 0xf) + 16;
223 for (c = 0; c < 8; c++)
224 {
225 buffer->line[pcjr->displine][c] = cols[0];
226 if (pcjr->array[0] & 1) buffer->line[pcjr->displine][c + (pcjr->crtc[1] << 3) + 8] = cols[0];
227 else buffer->line[pcjr->displine][c + (pcjr->crtc[1] << 4) + 8] = cols[0];
228 }
230 switch (pcjr->addr_mode)
231 {
232 case 0: /*Alpha*/
233 offset = 0;
234 mask = 0x3fff;
235 break;
236 case 1: /*Low resolution graphics*/
237 offset = (pcjr->sc & 1) * 0x2000;
238 break;
239 case 3: /*High resolution graphics*/
240 offset = (pcjr->sc & 3) * 0x2000;
241 break;
242 }
243 switch ((pcjr->array[0] & 0x13) | ((pcjr->array[3] & 0x08) << 5))
244 {
245 case 0x13: /*320x200x16*/
246 for (x = 0; x < pcjr->crtc[1]; x++)
247 {
248 dat = (pcjr->vram[((pcjr->ma << 1) & mask) + offset] << 8) |
249 pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
250 pcjr->ma++;
251 buffer->line[pcjr->displine][(x << 3) + 8] =
252 buffer->line[pcjr->displine][(x << 3) + 9] = pcjr->array[((dat >> 12) & pcjr->array[1]) + 16] + 16;
253 buffer->line[pcjr->displine][(x << 3) + 10] =
254 buffer->line[pcjr->displine][(x << 3) + 11] = pcjr->array[((dat >> 8) & pcjr->array[1]) + 16] + 16;
255 buffer->line[pcjr->displine][(x << 3) + 12] =
256 buffer->line[pcjr->displine][(x << 3) + 13] = pcjr->array[((dat >> 4) & pcjr->array[1]) + 16] + 16;
257 buffer->line[pcjr->displine][(x << 3) + 14] =
258 buffer->line[pcjr->displine][(x << 3) + 15] = pcjr->array[(dat & pcjr->array[1]) + 16] + 16;
259 }
260 break;
261 case 0x12: /*160x200x16*/
262 for (x = 0; x < pcjr->crtc[1]; x++)
263 {
264 dat = (pcjr->vram[((pcjr->ma << 1) & mask) + offset] << 8) |
265 pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
266 pcjr->ma++;
267 buffer->line[pcjr->displine][(x << 4) + 8] =
268 buffer->line[pcjr->displine][(x << 4) + 9] =
269 buffer->line[pcjr->displine][(x << 4) + 10] =
270 buffer->line[pcjr->displine][(x << 4) + 11] = pcjr->array[((dat >> 12) & pcjr->array[1]) + 16] + 16;
271 buffer->line[pcjr->displine][(x << 4) + 12] =
272 buffer->line[pcjr->displine][(x << 4) + 13] =
273 buffer->line[pcjr->displine][(x << 4) + 14] =
274 buffer->line[pcjr->displine][(x << 4) + 15] = pcjr->array[((dat >> 8) & pcjr->array[1]) + 16] + 16;
275 buffer->line[pcjr->displine][(x << 4) + 16] =
276 buffer->line[pcjr->displine][(x << 4) + 17] =
277 buffer->line[pcjr->displine][(x << 4) + 18] =
278 buffer->line[pcjr->displine][(x << 4) + 19] = pcjr->array[((dat >> 4) & pcjr->array[1]) + 16] + 16;
279 buffer->line[pcjr->displine][(x << 4) + 20] =
280 buffer->line[pcjr->displine][(x << 4) + 21] =
281 buffer->line[pcjr->displine][(x << 4) + 22] =
282 buffer->line[pcjr->displine][(x << 4) + 23] = pcjr->array[(dat & pcjr->array[1]) + 16] + 16;
283 }
284 break;
285 case 0x03: /*640x200x4*/
286 for (x = 0; x < pcjr->crtc[1]; x++)
287 {
288 dat = (pcjr->vram[((pcjr->ma << 1) & mask) + offset] << 8) |
289 pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
290 pcjr->ma++;
291 for (c = 0; c < 8; c++)
292 {
293 chr = (dat >> 7) & 1;
294 chr |= ((dat >> 14) & 2);
295 buffer->line[pcjr->displine][(x << 3) + 8 + c] = pcjr->array[(chr & pcjr->array[1]) + 16] + 16;
296 dat <<= 1;
297 }
298 }
299 break;
300 case 0x01: /*80 column text*/
301 for (x = 0; x < pcjr->crtc[1]; x++)
302 {
303 chr = pcjr->vram[((pcjr->ma << 1) & mask) + offset];
304 attr = pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
305 drawcursor = ((pcjr->ma == ca) && pcjr->con && pcjr->cursoron);
306 if (pcjr->array[3] & 4)
307 {
308 cols[1] = pcjr->array[ ((attr & 15) & pcjr->array[1]) + 16] + 16;
309 cols[0] = pcjr->array[(((attr >> 4) & 7) & pcjr->array[1]) + 16] + 16;
310 if ((pcjr->blink & 16) && (attr & 0x80) && !drawcursor)
311 cols[1] = cols[0];
312 }
313 else
314 {
315 cols[1] = pcjr->array[((attr & 15) & pcjr->array[1]) + 16] + 16;
316 cols[0] = pcjr->array[((attr >> 4) & pcjr->array[1]) + 16] + 16;
317 }
318 if (pcjr->sc & 8)
319 {
320 for (c = 0; c < 8; c++)
321 buffer->line[pcjr->displine][(x << 3) + c + 8] = cols[0];
322 }
323 else
324 {
325 for (c = 0; c < 8; c++)
326 buffer->line[pcjr->displine][(x << 3) + c + 8] = cols[(fontdat[chr][pcjr->sc & 7] & (1 << (c ^ 7))) ? 1 : 0];
327 }
328 // if (!((ma^(crtc[15]|(crtc[14]<<8)))&0x3FFF)) printf("Cursor match! %04X\n",ma);
329 if (drawcursor)
330 {
331 for (c = 0; c < 8; c++)
332 buffer->line[pcjr->displine][(x << 3) + c + 8] ^= 15;
333 }
334 pcjr->ma++;
335 }
336 break;
337 case 0x00: /*40 column text*/
338 for (x = 0; x < pcjr->crtc[1]; x++)
339 {
340 chr = pcjr->vram[((pcjr->ma << 1) & mask) + offset];
341 attr = pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
342 drawcursor = ((pcjr->ma == ca) && pcjr->con && pcjr->cursoron);
343 if (pcjr->array[3] & 4)
344 {
345 cols[1] = pcjr->array[ ((attr & 15) & pcjr->array[1]) + 16] + 16;
346 cols[0] = pcjr->array[(((attr >> 4) & 7) & pcjr->array[1]) + 16] + 16;
347 if ((pcjr->blink & 16) && (attr & 0x80) && !drawcursor)
348 cols[1] = cols[0];
349 }
350 else
351 {
352 cols[1] = pcjr->array[((attr & 15) & pcjr->array[1]) + 16] + 16;
353 cols[0] = pcjr->array[((attr >> 4) & pcjr->array[1]) + 16] + 16;
354 }
355 pcjr->ma++;
356 if (pcjr->sc & 8)
357 {
358 for (c = 0; c < 8; c++)
359 buffer->line[pcjr->displine][(x << 4) + (c << 1) + 8] =
360 buffer->line[pcjr->displine][(x << 4) + (c << 1) + 1 + 8] = cols[0];
361 }
362 else
363 {
364 for (c = 0; c < 8; c++)
365 buffer->line[pcjr->displine][(x << 4) + (c << 1) + 8] =
366 buffer->line[pcjr->displine][(x << 4) + (c << 1) + 1 + 8] = cols[(fontdat[chr][pcjr->sc & 7] & (1 << (c ^ 7))) ? 1 : 0];
367 }
368 if (drawcursor)
369 {
370 for (c = 0; c < 16; c++)
371 buffer->line[pcjr->displine][(x << 4) + c + 8] ^= 15;
372 }
373 }
374 break;
375 case 0x02: /*320x200x4*/
376 cols[0] = pcjr->array[0 + 16] + 16;
377 cols[1] = pcjr->array[1 + 16] + 16;
378 cols[2] = pcjr->array[2 + 16] + 16;
379 cols[3] = pcjr->array[3 + 16] + 16;
380 for (x = 0; x < pcjr->crtc[1]; x++)
381 {
382 dat = (pcjr->vram[((pcjr->ma << 1) & mask) + offset] << 8) |
383 pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
384 pcjr->ma++;
385 for (c = 0; c < 8; c++)
386 {
387 buffer->line[pcjr->displine][(x << 4) + (c << 1) + 8] =
388 buffer->line[pcjr->displine][(x << 4) + (c << 1) + 1 + 8] = cols[dat >> 14];
389 dat <<= 2;
390 }
391 }
392 break;
393 case 0x102: /*640x200x2*/
394 cols[0] = pcjr->array[0 + 16] + 16;
395 cols[1] = pcjr->array[1 + 16] + 16;
396 for (x = 0; x < pcjr->crtc[1]; x++)
397 {
398 dat = (pcjr->vram[((pcjr->ma << 1) & mask) + offset] << 8) |
399 pcjr->vram[((pcjr->ma << 1) & mask) + offset + 1];
400 pcjr->ma++;
401 for (c = 0; c < 16; c++)
402 {
403 buffer->line[pcjr->displine][(x << 4) + c + 8] = cols[dat >> 15];
404 dat <<= 1;
405 }
406 }
407 break;
408 }
409 }
410 else
411 {
412 if (pcjr->array[3] & 4)
413 {
414 if (pcjr->array[0] & 1) hline(buffer, 0, pcjr->displine, (pcjr->crtc[1] << 3) + 16, (pcjr->array[2] & 0xf) + 16);
415 else hline(buffer, 0, pcjr->displine, (pcjr->crtc[1] << 4) + 16, (pcjr->array[2] & 0xf) + 16);
416 }
417 else
418 {
419 // cols[0] = ((pcjr->mode & 0x12) == 0x12) ? 0 : (pcjr->col & 0xf) + 16;
420 cols[0] = pcjr->array[0 + 16] + 16;
421 if (pcjr->array[0] & 1) hline(buffer, 0, pcjr->displine, (pcjr->crtc[1] << 3) + 16, cols[0]);
422 else hline(buffer, 0, pcjr->displine, (pcjr->crtc[1] << 4) + 16, cols[0]);
423 }
424 }
425 if (pcjr->array[0] & 1) x = (pcjr->crtc[1] << 3) + 16;
426 else x = (pcjr->crtc[1] << 4) + 16;
427 if (cga_comp)
428 {
429 for (c = 0; c < x; c++)
430 {
431 y_buf[(c << 1) & 6] = ntsc_col[buffer->line[pcjr->displine][c] & 7][(c << 1) & 6] ? 0x6000 : 0;
432 y_buf[(c << 1) & 6] += (buffer->line[pcjr->displine][c] & 8) ? 0x3000 : 0;
433 i_buf[(c << 1) & 6] = y_buf[(c << 1) & 6] * i_filt[(c << 1) & 6];
434 q_buf[(c << 1) & 6] = y_buf[(c << 1) & 6] * q_filt[(c << 1) & 6];
435 y_tot = y_buf[0] + y_buf[1] + y_buf[2] + y_buf[3] + y_buf[4] + y_buf[5] + y_buf[6] + y_buf[7];
436 i_tot = i_buf[0] + i_buf[1] + i_buf[2] + i_buf[3] + i_buf[4] + i_buf[5] + i_buf[6] + i_buf[7];
437 q_tot = q_buf[0] + q_buf[1] + q_buf[2] + q_buf[3] + q_buf[4] + q_buf[5] + q_buf[6] + q_buf[7];
439 y_val = y_tot >> 10;
440 if (y_val > 255) y_val = 255;
441 y_val <<= 16;
442 i_val = i_tot >> 12;
443 if (i_val > 39041) i_val = 39041;
444 if (i_val < -39041) i_val = -39041;
445 q_val = q_tot >> 12;
446 if (q_val > 34249) q_val = 34249;
447 if (q_val < -34249) q_val = -34249;
449 r = (y_val + 249*i_val + 159*q_val) >> 16;
450 g = (y_val - 70*i_val - 166*q_val) >> 16;
451 b = (y_val - 283*i_val + 436*q_val) >> 16;
453 y_buf[((c << 1) & 6) + 1] = ntsc_col[buffer->line[pcjr->displine][c] & 7][((c << 1) & 6) + 1] ? 0x6000 : 0;
454 y_buf[((c << 1) & 6) + 1] += (buffer->line[pcjr->displine][c] & 8) ? 0x3000 : 0;
455 i_buf[((c << 1) & 6) + 1] = y_buf[((c << 1) & 6) + 1] * i_filt[((c << 1) & 6) + 1];
456 q_buf[((c << 1) & 6) + 1] = y_buf[((c << 1) & 6) + 1] * q_filt[((c << 1) & 6) + 1];
457 y_tot = y_buf[0] + y_buf[1] + y_buf[2] + y_buf[3] + y_buf[4] + y_buf[5] + y_buf[6] + y_buf[7];
458 i_tot = i_buf[0] + i_buf[1] + i_buf[2] + i_buf[3] + i_buf[4] + i_buf[5] + i_buf[6] + i_buf[7];
459 q_tot = q_buf[0] + q_buf[1] + q_buf[2] + q_buf[3] + q_buf[4] + q_buf[5] + q_buf[6] + q_buf[7];
461 y_val = y_tot >> 10;
462 if (y_val > 255) y_val = 255;
463 y_val <<= 16;
464 i_val = i_tot >> 12;
465 if (i_val > 39041) i_val = 39041;
466 if (i_val < -39041) i_val = -39041;
467 q_val = q_tot >> 12;
468 if (q_val > 34249) q_val = 34249;
469 if (q_val < -34249) q_val = -34249;
471 r = (y_val + 249*i_val + 159*q_val) >> 16;
472 g = (y_val - 70*i_val - 166*q_val) >> 16;
473 b = (y_val - 283*i_val + 436*q_val) >> 16;
474 if (r > 511) r = 511;
475 if (g > 511) g = 511;
476 if (b > 511) b = 511;
478 ((uint32_t *)buffer32->line[pcjr->displine])[c] = makecol32(r / 2, g / 2, b / 2);
479 }
480 }
481 pcjr->sc = oldsc;
482 if (pcjr->vc == pcjr->crtc[7] && !pcjr->sc)
483 {
484 pcjr->stat |= 8;
485 // printf("VSYNC on %i %i\n",vc,sc);
486 }
487 pcjr->displine++;
488 if (pcjr->displine >= 360)
489 pcjr->displine = 0;
490 }
491 else
492 {
493 pcjr->vidtime += pcjr->dispontime;
494 if (pcjr->dispon)
495 pcjr->stat |= 1;
496 pcjr->linepos = 0;
497 if (pcjr->vsynctime)
498 {
499 pcjr->vsynctime--;
500 if (!pcjr->vsynctime)
501 {
502 pcjr->stat &= ~8;
503 // printf("VSYNC off %i %i\n",vc,sc);
504 }
505 }
506 if (pcjr->sc == (pcjr->crtc[11] & 31) || ((pcjr->crtc[8] & 3) == 3 && pcjr->sc == ((pcjr->crtc[11] & 31) >> 1)))
507 {
508 pcjr->con = 0;
509 pcjr->coff = 1;
510 }
511 if (pcjr->vadj)
512 {
513 pcjr->sc++;
514 pcjr->sc &= 31;
515 pcjr->ma = pcjr->maback;
516 pcjr->vadj--;
517 if (!pcjr->vadj)
518 {
519 pcjr->dispon = 1;
520 pcjr->ma = pcjr->maback = (pcjr->crtc[13] | (pcjr->crtc[12] << 8)) & 0x3fff;
521 pcjr->sc = 0;
522 // printf("Display on!\n");
523 }
524 }
525 else if (pcjr->sc == pcjr->crtc[9] || ((pcjr->crtc[8] & 3) == 3 && pcjr->sc == (pcjr->crtc[9] >> 1)))
526 {
527 pcjr->maback = pcjr->ma;
528 // con=0;
529 // coff=0;
530 pcjr->sc = 0;
531 oldvc = pcjr->vc;
532 pcjr->vc++;
533 pcjr->vc &= 127;
534 // pclog("VC %i %i\n", pcjr->vc, pcjr->crtc[7]);
535 // printf("VC %i %i %i %i %i\n",vc,crtc[4],crtc[6],crtc[7],pcjr->dispon);
536 if (pcjr->vc == pcjr->crtc[6])
537 pcjr->dispon = 0;
538 if (oldvc == pcjr->crtc[4])
539 {
540 // printf("Display over at %i\n",pcjr->displine);
541 pcjr->vc = 0;
542 pcjr->vadj = pcjr->crtc[5];
543 if (!pcjr->vadj)
544 pcjr->dispon = 1;
545 if (!pcjr->vadj)
546 pcjr->ma = pcjr->maback = (pcjr->crtc[13] | (pcjr->crtc[12] << 8)) & 0x3fff;
547 if ((pcjr->crtc[10] & 0x60) == 0x20) pcjr->cursoron = 0;
548 else pcjr->cursoron = pcjr->blink & 16;
549 // printf("CRTC10 %02X %i\n",crtc[10],cursoron);
550 }
551 if (pcjr->vc == pcjr->crtc[7])
552 {
553 pcjr->dispon = 0;
554 pcjr->displine = 0;
555 pcjr->vsynctime = 16;//(crtc[3]>>4)+1;
556 picint(1 << 5);
557 // printf("pcjr->vsynctime %i %02X\n",pcjr->vsynctime,crtc[3]);
558 // pcjr->stat|=8;
559 if (pcjr->crtc[7])
560 {
561 // printf("Lastline %i Firstline %i %i %i %i\n",lastline,firstline,lastline-firstline,crtc[1],xsize);
562 if (pcjr->array[0] & 1) x = (pcjr->crtc[1] << 3) + 16;
563 else x = (pcjr->crtc[1] << 4) + 16;
564 pcjr->lastline++;
565 if (x != xsize || (pcjr->lastline - pcjr->firstline) != ysize)
566 {
567 xsize = x;
568 ysize = pcjr->lastline - pcjr->firstline;
569 // printf("Resize to %i,%i - R1 %i\n",xsize,ysize,crtc[1]);
570 if (xsize < 64) xsize = 656;
571 if (ysize < 32) ysize = 200;
572 updatewindowsize(xsize, (ysize << 1) + 16);
573 }
574 // printf("Blit %i %i\n",firstline,lastline);
575 //printf("Xsize is %i\n",xsize);
576 startblit();
577 if (cga_comp)
578 video_blit_memtoscreen(0, pcjr->firstline-4, 0, (pcjr->lastline - pcjr->firstline) + 8, xsize, (pcjr->lastline - pcjr->firstline) + 8);
579 else
580 video_blit_memtoscreen_8(0, pcjr->firstline-4, xsize, (pcjr->lastline - pcjr->firstline) + 8);
581 endblit();
582 frames++;
583 video_res_x = xsize - 16;
584 video_res_y = ysize;
585 }
586 pcjr->firstline = 1000;
587 pcjr->lastline = 0;
588 pcjr->blink++;
589 }
590 }
591 else
592 {
593 pcjr->sc++;
594 pcjr->sc &= 31;
595 pcjr->ma = pcjr->maback;
596 }
597 if ((pcjr->sc == (pcjr->crtc[10] & 31) || ((pcjr->crtc[8] & 3) == 3 && pcjr->sc == ((pcjr->crtc[10] & 31) >> 1))))
598 pcjr->con = 1;
599 }
600 }
602 static void *pcjr_video_init()
603 {
604 int c;
605 int pcjr_tint = -2;
606 pcjr_t *pcjr = malloc(sizeof(pcjr_t));
607 memset(pcjr, 0, sizeof(pcjr_t));
609 pcjr->memctrl = -1;
611 for (c = 0; c < 8; c++)
612 {
613 i_filt[c] = 512.0 * cos((3.14 * (pcjr_tint + c * 4) / 16.0) - 33.0 / 180.0);
614 q_filt[c] = 512.0 * sin((3.14 * (pcjr_tint + c * 4) / 16.0) - 33.0 / 180.0);
615 }
616 timer_add(pcjr_poll, &pcjr->vidtime, TIMER_ALWAYS_ENABLED, pcjr);
617 mem_mapping_add(&pcjr->mapping, 0xb8000, 0x08000, pcjr_read, NULL, NULL, pcjr_write, NULL, NULL, NULL, 0, pcjr);
618 io_sethandler(0x03d0, 0x0010, pcjr_in, NULL, NULL, pcjr_out, NULL, NULL, pcjr);
619 return pcjr;
620 }
622 static void pcjr_video_close(void *p)
623 {
624 pcjr_t *pcjr = (pcjr_t *)p;
626 free(pcjr);
627 }
629 static void pcjr_speed_changed(void *p)
630 {
631 pcjr_t *pcjr = (pcjr_t *)p;
633 pcjr_recalctimings(pcjr);
634 }
636 device_t pcjr_video_device =
637 {
638 "IBM PCjr (video)",
639 0,
640 pcjr_video_init,
641 pcjr_video_close,
642 NULL,
643 pcjr_speed_changed,
644 NULL,
645 NULL
646 };