-
Notifications
You must be signed in to change notification settings - Fork 7
/
Blitter.c
591 lines (433 loc) · 20 KB
/
Blitter.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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//
// Blitter.c
// The Omega Project
// https://github.com/h5n1xp/Omega
//
// Created by Matt Parsons on 20/02/2019.
// Copyright © 2019 Matt Parsons. All rights reserved.
// <[email protected]>
//
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed
// with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
//
/*
cycle 0: set up blitter;
if(mode = copy mode){
cycle = 0;
}
if(mode = fill mode){
cycle = 5;
}
if(mode = line mode){
cycle = -1
}
cycle 1: if(A needs load){
load A;
cycle = 2;
}else if{B needs to load){
load B;
cycle = 3;
} else if{C needs to load){
Load C;
cycle = 4;
}
cycle 2: if{B needs to load){
load B;
cycle = 3;
} else if{C needs to load){
Load C;
cycle = 4;
}
cycle 3: if{C needs to load){
Load C;
cycle = 4;
}
cycle 4: mask A;
Shift A;
Shift B;
apply logic functions
set clear zero flag
if(Store D needed){
Store D;
}
update v and h counters
cycle = 1 ; //repeat the operation
if(v == end condition){
reset v = starting value.
increment h
}
if(h == end conditon{
clear blitter busy flag
Generate interrupt;
cycle = 0; // rest the blitter state
}
cycle 5: fill mode
*/
#include "Blitter.h"
void blitterCycle(void){
}
void (*blitterOperation[]) (void) = {blitterCycle,blitterCycle};
int OblitterExecute(){
static int cycle;
//check if blitter DMA is on; if not then return 0
//Check if blitter needs to run (blitter busy flag is set); if not then return 0;
blitterOperation[cycle]();
return 1; //if we got here we probably used the memory cycle...
}
//********************* BLITER SECTION *****************************
uint16_t logicFunction(int minterm,uint16_t wordA, uint16_t wordB, uint16_t wordC){
//Not section
uint16_t notA = ~wordA;
uint16_t notB = ~wordB;
uint16_t notC = ~wordC;
uint16_t channelD = 0;
//Logic Section
if(minterm & 0x80){
channelD = (wordA & wordB & wordC);}
if(minterm & 0x40){
channelD = channelD | (wordA & wordB & notC);}
if(minterm & 0x20){
channelD = channelD | (wordA & notB & wordC);}
if(minterm & 0x10){
channelD = channelD | (wordA & notB & notC);}
if(minterm & 0x08){
channelD = channelD | (notA & wordB & wordC);}
if(minterm & 0x04){
channelD = channelD | (notA & wordB & notC);}
if(minterm & 0x02){
channelD = channelD | (notA & notB & wordC);}
if(minterm & 0x01){
channelD = channelD | (notA & notB & notC);}
return channelD;
}
void blitter_execute(Chipset_t* chipset){
//Blitter DMA Enabled and blitter busy flag set (which means bltsize was written to and blitting should start).
if( (chipset->dmaconr & 16960) == 16960 ){
/*
//make it so the blitter doesn't blit immediatly
static int delay;
delay -=1;
if(delay>0){
return;
}
delay = 10;
*/
if(chipset->bltcon1 & 1){
//Line Mode
//printf("Blitter Line Mode\n");
int octCode = (chipset->bltcon1 >> 2) & 7;
int length = chipset->bltsizv;
int inc1 = chipset->bltamod; // 4(dy - dx)
int D = (int16_t)chipset->bltapt;// start value of 4dy - 2dx
uint16_t* chipramW = internal.chipramW;
int planeAddr = chipset->bltcpt & 0x1FFFFE;//word address
int planeMod = chipset->bltcmod;
int inc2 = chipset->bltbmod;// 4dy
int d=0;
int startPixel = chipset->bltcon0 >>12;
int oneDot = (chipset->bltcon1 >>1) & 1; // I don't support one dot mode yet
if(oneDot==1){
printf("No Single pixel per H-line mode yet\n");
}
int minterm = chipset->bltcon0 & 255; //0xCA = normal 0x4A = XOR
int patternShift = chipset->bltcon1 >>12;
uint16_t pattern = chipset->bltbdat;
pattern = (pattern >> patternShift) | (pattern <<(16-patternShift));
int addr=0;//running address
//printf("Octant %d: dx - %d dy - %d @ 0x%06x\n",octCode,length,inc2/4,chipset->bltcpt);
switch(octCode){
case 0:
for(int i=0;i<length;++i){
int offset = d+startPixel;
addr = (planeAddr +(offset>>3)+(i*planeMod)) >> 1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x8000 >> (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] =pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
case 1:
for(int i=0;i<length;++i){
int offset = d+startPixel;
addr =(planeAddr +(offset>>3)-(i*planeMod)) >> 1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x8000 >> (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] =pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
case 2:
startPixel = 15 - startPixel;
planeAddr +=1;
for(int i=0;i<length;++i){
int offset = d+startPixel;
addr = (planeAddr - (offset>>3)+(i*planeMod))>>1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x0001 << (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] = pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
case 3:
for(int i=0;i<length;++i){
int offset =d+startPixel;
addr =(planeAddr +(offset>>3)-(i*planeMod))>>1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x8000 >> (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] =pixel;
if(D>0){
D = D + inc1;
d = d - 1;
}else{
D = D + inc2;
}
}
break;
case 4:
for(int i=0;i<length;++i){
int offset = i+startPixel;
addr =(planeAddr +(offset>>3)+(d*planeMod))>>1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x8000 >> (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] =pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
case 5:
startPixel = 15 - startPixel;
planeAddr +=1;
for(int i=0;i<length;++i){
int offset = i+startPixel;
addr = (planeAddr - (offset>>3)+(d*planeMod))>>1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x0001 << (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] = pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
case 6:
for(int i=0;i<length;++i){
int offset = i+startPixel;
addr =(planeAddr +(offset>>3)-(d*planeMod))>>1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x8000 >> (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] =pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
case 7:
startPixel = 15 - startPixel;
planeAddr +=1;
for(int i=0;i<length;++i){
int offset = i+startPixel;
addr = (planeAddr - (offset>>3)-(d*planeMod))>>1;
//Pixel plot
uint16_t pixel = chipramW[addr];
pixel = (pixel <<8) | (pixel >>8);
pixel = logicFunction(minterm,0x0001 << (offset&15),pattern,pixel);
pixel = (pixel <<8) | (pixel >>8);
chipramW[addr] = pixel;
if(D>0){
D = D + inc1;
d = d + 1;
}else{
D = D + inc2;
}
}
break;
default:
break;
}
chipset->bltcpt =addr; // update cpt with the last known address... nothing should rely on this...
//chipset->bltapt = D; // update apt with the last known address... nothing should rely on this...
chipset->bltsizh = 0; // all done;
chipset->bltsizv = 0; // all done;
chipset->dmaconr = chipset->dmaconr & 49151; //clear blitter busy bit
//generate an interrupt!
putChipReg16[INTREQ](0x8040);
}else{
static int count = 0;
//printf("Blitter Copy Mode: %d\n",count);
count++;
//Area Copy Blitter
//printf("blit %d: A-%06x (%d) B-%06x (%d) C-%06x (%d) D-%06x (%d) W-%d H-%d\n",count,chipset->bltapt,chipset->bltamod,chipset->bltbpt,chipset->bltbmod,chipset->bltcpt,chipset->bltcmod,chipset->bltdpt,chipset->bltdmod,chipset->bltsizh,chipset->bltsizv);
int useMask = chipset->bltcon0 >> 8;
int shiftA = chipset->bltcon0 >> 12;
int shiftB = chipset->bltcon1 >> 12;
int minterm = chipset->bltcon0 & 255;
int xIncrement = 1;
int fillmode = (chipset->bltcon1 & 0x18) >> 3; // 1= Inclusive Fill, 2 = Exclusive Fill
//if descend mode is on.
if((chipset->bltcon1 & 2)){
xIncrement = -1;
if(fillmode==1){
printf("NO INCLUSIVE FILL MODE YET!!\n");
}
if(fillmode==2){
printf("NO EXCLUSIVE FILL MODE YET!!\n");
}
}
//all pointers are word addressed and masked for 2meg only
int apt=(chipset->bltapt >> 1);
int bpt=(chipset->bltbpt >> 1);
int cpt=(chipset->bltcpt >> 1);
int dpt=(chipset->bltdpt >> 1);
//Shifted seems to work properly
int amod=chipset->bltamod >> 1;
int bmod=chipset->bltbmod >> 1;
int cmod=chipset->bltcmod >> 1;
int dmod=chipset->bltdmod >> 1;
//these are signed so need to be divided not shifted... but doesn't work properly
//int amod=chipset->bltamod / 2;
//int bmod=chipset->bltbmod / 2;
//int cmod=chipset->bltcmod / 2;
//int dmod=chipset->bltdmod / 2;
uint16_t adat=chipset->bltadat;
uint16_t bdat=chipset->bltbdat;
uint16_t cdat=chipset->bltcdat;
// int ddat=chipset->bltddat;
uint16_t afwm = chipset->bltafwm;
uint16_t alwm = chipset->bltalwm;
int sizeh = chipset->bltsizh;
int sizev = chipset->bltsizv;
int lastHWord=sizeh - 1;
//All operations start with a Zero Flag
chipset->dmaconr = chipset->dmaconr | 0x2000; //set zero flag
for(int y=0;y<sizev;++y){
uint16_t previousA = 0;
uint16_t previousB = 0;
for(int x=0;x<sizeh;++x){
uint16_t channelD = 0;
//Channel A
if(useMask & 8){
adat = internal.chipramW[apt];
adat = adat << 8 | adat >> 8;
apt +=xIncrement;
}
uint16_t channelA = adat;
//Channel B
if(useMask & 4){
bdat = internal.chipramW[bpt];
bdat = bdat << 8 | bdat >> 8;
bpt +=xIncrement;
}
uint16_t channelB = bdat;
//Channel C
if(useMask & 2){
cdat = internal.chipramW[cpt];
cdat = cdat << 8 | cdat >> 8;
cpt +=xIncrement;
}
//Masking section
if(x==0){
channelA = channelA & afwm;
}
if(x==lastHWord){
channelA = channelA & alwm;
}
uint16_t A = channelA; //Need to record the masked A for next cycle (Thanks to aros-sg on eab)
//shifting section
if(xIncrement==-1){
channelA = (previousA >> (16-shiftA)) | (channelA << shiftA);
channelB = (previousB >> (16-shiftB)) | (channelB << shiftB);
}else{
channelA = (previousA << (16-shiftA)) | (channelA >> shiftA);
channelB = (previousB << (16-shiftB)) | (channelB >> shiftB);
}
//previousA = adat;
previousA = A;
previousB = bdat;
channelD = logicFunction(minterm, channelA, channelB, cdat);
//Zero Flag
if(channelD!=0){
chipset->dmaconr = chipset->dmaconr & 0xDFFF; // clear zero flag
}
//Channel D
if(useMask & 1){
channelD = channelD << 8 | channelD >>8;
internal.chipramW[dpt] = channelD;
dpt +=xIncrement;
}
}
if(xIncrement == -1){
apt -= amod;
bpt -= bmod;
cpt -= cmod;
dpt -= dmod;
}else{
apt += amod;
bpt += bmod;
cpt += cmod;
dpt += dmod;
}
}
chipset->bltsizh = 0; // all done;
chipset->bltsizv = 0; // all done;
//save blitter state - something might depend upon the blitter being in a known state
chipset->bltapt = apt << 1;
chipset->bltbpt = bpt << 1;
chipset->bltcpt = cpt << 1;
chipset->bltdpt = dpt << 1;
chipset->bltadat = adat;
chipset->bltbdat = bdat;
chipset->bltcdat = cdat;
//chipset->bltddat = ddat;
chipset->dmaconr = chipset->dmaconr & 0xBFFF; //clear blitter busy bit
putChipReg16[INTREQ](0x8040); // generate an interrupt!
return;
}
}
}