forked from pete-gordon/oricutron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathula.c
480 lines (413 loc) · 12.6 KB
/
ula.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
** Oricutron
** Copyright (C) 2009-2014 Peter Gordon
**
** This program is free software; you can redistribute it and/or
** modify it under the terms of the GNU General Public License
** as published by the Free Software Foundation, version 2
** of the License.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
** Oric video ULA
*/
#include <stdlib.h>
#include <string.h>
#include "system.h"
#include "6502.h"
#include "via.h"
#include "8912.h"
#include "disk.h"
#include "gui.h"
#include "monitor.h"
#include "6551.h"
#include "machine.h"
#include "ula.h"
#include "avi.h"
extern struct avi_handle *vidcap;
extern SDL_bool warpspeed;
static unsigned char bittab[8*8*64*8];
// Refresh the video base pointer
static inline void ula_refresh_charset( struct machine *oric )
{
if( oric->vid_textattrs & 1 )
{
oric->vid_ch_data = &oric->vid_ch_base[128*8];
} else {
oric->vid_ch_data = oric->vid_ch_base;
}
}
// Decode an oric video attribute
static inline void ula_decode_attr( struct machine *oric, int attr, int y )
{
switch( attr & 0x18 )
{
case 0x00: // Foreground colour
oric->vid_fg_col = attr & 0x07;
oric->vid_bitptr = (Uint16 *)(&bittab[(oric->vid_fg_col<<12) | (oric->vid_bg_col<<9)]);
oric->vid_inv_bitptr = (Uint16 *)(&bittab[((oric->vid_fg_col^7)<<12) | ((oric->vid_bg_col^7)<<9)]);
break;
case 0x08: // Text attribute
oric->vid_textattrs = attr & 0x07;
oric->vid_blinkmask = attr & 0x04 ? 0x00 : 0x3f;
ula_refresh_charset( oric );
if( oric->vid_textattrs & 0x02 )
oric->vid_chline = (y>>1) & 0x07;
else
oric->vid_chline = y & 0x07;
break;
case 0x10: // Background colour
oric->vid_bg_col = attr & 0x07;
oric->vid_bitptr = (Uint16 *)(&bittab[(oric->vid_fg_col<<12) | (oric->vid_bg_col<<9)]);
oric->vid_inv_bitptr = (Uint16 *)(&bittab[((oric->vid_fg_col^7)<<12) | ((oric->vid_bg_col^7)<<9)]);
break;
case 0x18: // Video mode
oric->vid_mode = attr & 0x07;
// Set up pointers for new mode
if( oric->vid_mode & 4 )
{
oric->vid_addr = oric->vidbases[0];
oric->vid_ch_base = &oric->mem[oric->vidbases[1]];
} else {
oric->vid_addr = oric->vidbases[2];
oric->vid_ch_base = &oric->mem[oric->vidbases[3]];
}
ula_refresh_charset( oric );
break;
}
}
static inline void ula_raster_default( struct machine *oric )
{
oric->vid_fg_col = 7;
oric->vid_textattrs = 0;
oric->vid_blinkmask = 0x3f;
oric->vid_bg_col = 0;
oric->vid_bitptr = (Uint16 *)(&bittab[(oric->vid_fg_col<<12) | (oric->vid_bg_col<<9)]);
oric->vid_inv_bitptr = (Uint16 *)(&bittab[((oric->vid_fg_col^7)<<12) | ((oric->vid_bg_col^7)<<9)]);
ula_refresh_charset( oric );
}
void ula_powerup_default( struct machine *oric )
{
ula_decode_attr( oric, 0x1a, 0 );
}
// Render a 6x1 block
static void ula_render_block( struct machine *oric, SDL_bool inverted, int data, int y )
{
Uint16 *ptr;
if( inverted )
{
ptr = oric->vid_inv_bitptr + (data<<2);
}
else
{
ptr = oric->vid_bitptr + (data<<2);
}
*((Uint16 *)oric->scrpt) = *(ptr++); oric->scrpt+=2;
*((Uint16 *)oric->scrpt) = *(ptr++); oric->scrpt+=2;
*((Uint16 *)oric->scrpt) = *(ptr++); oric->scrpt+=2;
}
static void ula_render_block_checkdirty( struct machine *oric, SDL_bool inverted, int data, int y )
{
Uint16 *ptr;
int i;
if( inverted )
{
ptr = oric->vid_inv_bitptr + (data<<2);
}
else
{
ptr = oric->vid_bitptr + (data<<2);
}
for( i=0; i<3; i++)
{
if (*((Uint16 *)oric->scrpt) != *ptr)
{
*((Uint16 *)oric->scrpt) = *ptr;
oric->vid_dirty[y] = SDL_TRUE;
oric->vid_block_func = ula_render_block;
}
oric->scrpt+=2;
ptr++;
}
}
// Render current screen (used by the monitor)
void ula_renderscreen( struct machine *oric )
{
Uint8 *scrpt, *rptr;
int b, y, c, cy, bitmask;
SDL_bool hires;
scrpt = oric->scrpt;
oric->scrpt = oric->scr;
for( y=0; y<224; y++)
{
cy = (y>>3) * 40;
// Always start each scanline with white on black
ula_raster_default( oric );
oric->vid_chline = y & 0x07;
if( y < 200 )
{
if( oric->vid_mode & 0x04 ) // HIRES?
{
hires = SDL_TRUE;
rptr = &oric->mem[oric->vid_addr + y*40 -1];
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vid_addr + cy -1];
}
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vidbases[2] + cy -1]; // bb80 = bf68 - (200/8*40)
}
bitmask = (oric->frames&0x10)?0x3f:oric->vid_blinkmask;
for( b=0; b<40; b++ )
{
c = *(++rptr);
/* if bits 6 and 5 are zero, the byte contains a serial attribute */
if( ( c & 0x60 ) == 0 )
{
ula_decode_attr( oric, c, y );
ula_render_block( oric, (c & 0x80)!=0, 0, y );
if( y < 200 )
{
if( oric->vid_mode & 0x04 ) // HIRES?
{
hires = SDL_TRUE;
rptr = &oric->mem[oric->vid_addr + b + y*40];
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vid_addr + b + cy];
}
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vidbases[2] + b + cy]; // bb80 = bf68 - (200/8*40)
}
bitmask = (oric->frames&0x10)?0x3f:oric->vid_blinkmask;
} else {
if( hires )
{
ula_render_block( oric, (c & 0x80)!=0, c & bitmask, y );
} else {
int ch_ix, ch_dat;
ch_ix = c & 0x7f;
ch_dat = oric->vid_ch_data[ (ch_ix<<3) | oric->vid_chline ] & bitmask;
ula_render_block( oric, (c & 0x80)!=0, ch_dat, y );
}
}
}
}
oric->scrpt = scrpt;
}
// Draw one rasterline
SDL_bool ula_doraster( struct machine *oric )
{
int b, c, bitmask;
SDL_bool hires, needrender;
unsigned int y, cy;
Uint8 *rptr;
needrender = SDL_FALSE;
oric->vid_raster++;
if( oric->vid_raster == oric->vid_maxrast )
{
if( vidcap )
{
// If we're recording with sound, and they do warp speed,
// stop writing frames to the AVI, since it'll just get out of sync.
if ( ( !vidcap->dosnd ) || ( !warpspeed ) )
{
// If the oric refresh rate and AVI refresh rate match, just output every frame
if( vidcap->is50hz == oric->vid_freq )
{
ay_lockaudio( &oric->ay ); // Gets unlocked at the end of each frame
avi_addframe( &vidcap, oric->scr );
}
// Check for 60hz oric & 50 hz AVI
else if( vidcap->is50hz )
{
// In this case we need to throw away every sixth frame
if( (vidcap->frameadjust%6) != 5 )
{
ay_lockaudio( &oric->ay ); // Gets unlocked at the end of each frame
avi_addframe( &vidcap, oric->scr );
}
vidcap->frameadjust++;
}
// Must be 50hz oric & 60 hz AVI
else
{
// In this case we need to duplicate every fifth frame
ay_lockaudio( &oric->ay ); // Gets unlocked at the end of each frame
avi_addframe( &vidcap, oric->scr );
if( (vidcap->frameadjust%5) == 4 )
avi_addframe( &vidcap, oric->scr );
vidcap->frameadjust++;
}
}
}
oric->vid_raster = 0;
// store current T1 counter
oric->vid_offset = oric->via.t1c/64;
/*
* NOTE: this hard coded value is not correct:
* oric->vsync = oric->cyclesperraster / 2;
*
* VSync pulse waveform:
* ====================
*
* - on RGB output VSYNC: |___ _____ _____ _____
* | | | | | | | .....
* | |___________| |_| |_|
* |
* - on VIA pin 18 CB1: |________ ________________
* | | | .....
* |12uS -->|___________|
* |delay <---------> 260uS negative pulse
* |
* +-----------------------------------------> t
*/
oric->vsync = 12 + 260;
needrender = SDL_TRUE;
oric->frames++;
if( oric->vid_freq != (oric->vid_mode&2) )
{
oric->vid_freq = oric->vid_mode&2;
// PAL50 = 50Hz = 1,000,000/50 = 20000 cpu cycles/frame
// 312 scanlines/frame, so 20000/312 = ~64 cpu cycles / scanline
// PAL60 = 60Hz = 1,000,000/60 = 16667 cpu cycles/frame
// 312 scanlines/frame, so 16667/312 = ~53 cpu cycles / scanline
// NTSC = 60Hz = 1,000,000/60 = 16667 cpu cycles/frame
// 264 scanlines/frame, so 16667/260 = ~64 cpu cycles / scanline
if( oric->vid_freq )
{
// PAL50 - T1 period 19966
oric->cyclesperraster = 64;
oric->vid_maxrast = 308 + 4; /* VSync */
oric->vid_start = (oric->vid_maxrast - 224) /2;
oric->vid_end = oric->vid_start + 224;
} else {
// NTSC - T1 period 16894
oric->cyclesperraster = 64;
oric->vid_maxrast = 260 + 4; /* VSync */
oric->vid_start = (oric->vid_maxrast - 224) /2;
oric->vid_end = oric->vid_start + 224;
}
}
}
// Are we on a visible rasterline?
if( ( oric->vid_raster < oric->vid_start ) ||
( oric->vid_raster >= oric->vid_end ) ) return needrender;
y = oric->vid_raster - oric->vid_start;
oric->scrpt = &oric->scr[y*240];
cy = (y>>3) * 40;
// Always start each scanline with white on black
ula_raster_default( oric );
oric->vid_chline = y & 0x07;
// warpspeed does frameskipping, so lines may still be dirty
oric->vid_block_func = oric->vid_dirty[y] ? ula_render_block : ula_render_block_checkdirty;
if( y < 200 )
{
if( oric->vid_mode & 0x04 ) // HIRES?
{
hires = SDL_TRUE;
rptr = &oric->mem[oric->vid_addr + y*40 -1];
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vid_addr + cy -1];
}
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vidbases[2] + cy -1]; // bb80 = bf68 - (200/8*40)
}
bitmask = (oric->frames&0x10)?0x3f:oric->vid_blinkmask;
for( b=0; b<40; b++ )
{
c = *(++rptr);
/* if bits 6 and 5 are zero, the byte contains a serial attribute */
if( ( c & 0x60 ) == 0 )
{
ula_decode_attr( oric, c, y );
oric->vid_block_func( oric, (c & 0x80)!=0, 0, y );
if( y < 200 )
{
if( oric->vid_mode & 0x04 ) // HIRES?
{
hires = SDL_TRUE;
rptr = &oric->mem[oric->vid_addr + b + y*40];
} else {
hires = SDL_FALSE;
rptr = &oric->mem[oric->vid_addr + b + cy];
}
} else {
if (hires)
{
hires = SDL_FALSE;
rptr = &oric->mem[oric->vidbases[2] + b + cy]; // bb80 = bf68 - (200/8*40)
}
}
bitmask = (oric->frames&0x10)?0x3f:oric->vid_blinkmask;
} else {
if( hires )
{
oric->vid_block_func( oric, (c & 0x80)!=0, c & bitmask, y );
} else {
int ch_ix, ch_dat;
ch_ix = c & 0x7f;
ch_dat = oric->vid_ch_data[ (ch_ix<<3) | oric->vid_chline ] & bitmask;
oric->vid_block_func( oric, (c & 0x80)!=0, ch_dat, y );
}
}
}
return needrender;
}
void ula_set_dirty( struct machine *oric )
{
int i;
for( i=0; i<224; i++ )
{
oric->vid_dirty[i] = SDL_TRUE;
}
}
void preinit_ula( struct machine *oric )
{
oric->scr = NULL;
oric->aratio = SDL_TRUE;
oric->hstretch = SDL_TRUE;
oric->scanlines = SDL_FALSE;
oric->palghost = SDL_TRUE;
}
SDL_bool init_ula( struct machine *oric )
{
int fg, bg, bits, offs, mask;
oric->scr = (Uint8 *)malloc( 240*224 );
if( !oric->scr ) return SDL_FALSE;
memset(oric->scr, 0, 240*224);
ula_set_dirty( oric );
/* Precalc all 6 bit combinations for all colour combinations */
for( fg=0; fg<8; fg++ )
{
for( bg=0; bg<8; bg++ )
{
for( bits=0; bits<64; bits++)
{
// FFFBBBbbbbbb000
offs = (fg<<12)|(bg<<9)|(bits<<3);
for( mask=0x20; mask; mask>>=1 )
{
bittab[offs++] = (bits&mask) ? fg : bg;
}
}
}
}
return SDL_TRUE;
}
void shut_ula( struct machine *oric )
{
if( oric->scr ) free( oric->scr );
oric->scr = NULL;
}