-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvga.c
672 lines (506 loc) · 14.4 KB
/
vga.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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
//name of bmp file
const char FileName[50];
void VGAdrawvertline(int length, int xorg, int yorg, short colour);
void VGAdrawhorzline(int length, int xorg, int yorg, short colour);
void drawArrayofBoxes(int x,int y, int sidelength,short colour);
void drawBox(int x, int y, int sidelength, short colour);
void drawfilledBox(int x, int y, int lengthx, int lengthy, short colour);
void blink(int);
void write_tomainscore(int score);
#define endofscreenx 320
#define endofscreeny 240
#define pixelbufferx 80
#define pixelbuffery 60
//int player;
//14 bytes
typedef struct tagBITMAPFILEHEADER
{
//2 bytes
char bfType[2];
//4 bytes
unsigned int bfSize;
// 2 bytes
unsigned short int bfReserved1;
// 2 bytes
unsigned short int bfReserved2;
// 4 bytes
unsigned int bfOffBits;
} BITMAPFILEHEADER, *PBITMAPFILEHEADER;
//40 bytes
typedef struct tagBITMAPINFOHEADER
{
// 4 bytes
unsigned int biSize;
// 4 bytes
int biWidth;
// 4 bytes
int biHeight;
// 2 bytes
unsigned short int biPlanes;
// 2 bytes
unsigned short int biBitCount;
// 4 bytes
unsigned int biCompression;
// 4 bytes
unsigned int biSizeImage;
// 4 bytes
int biXPelsPerMeter;
// 4 bytes
int biYPelsPerMeter;
// 4 bytes
unsigned int biClrUsed;
// 4 bytes
unsigned int biClrImportant;
} BITMAPINFOHEADER, *PBITMAPINFOHEADER;
//colour info is afterwards
//functional prototypes
typedef struct
{
// 16-bits
short colour;
}
Colours;
//global variables
int height,width;
//check if it's a valid BMP 16-bit, as well as read the attributes data
void validBMP(FILE * file);
// retrieve height,width from info header
BITMAPINFOHEADER retrieveInfo(FILE * file);
//creates the matrix and dynamically allocate it
Colours ** createMatrix();
//
void loadImage(FILE * file, Colours ** matrix);
void VGAwriteline(char * str, int xpos,int ypos);
//write bmp pixel data to matrix
//void writeBMP(FILE * file, Colours ** matrix,BITMAPFILEHEADER);
/* set a single pixel on the screen at x,y
* x in [0,319], y in [0,239], and colour in [0,65535]
*/
void write_pixel(int x, int y, short colour) {
volatile short *vga_addr=(volatile short*)(0x08000000 + (y<<10) + (x<<1));
*vga_addr=colour;
}
/* use write_pixel to set entire screen to black (does not clear the character buffer) */
void clear_screen() {
int x, y;
for (x = 0; x < 320; x++) {
for (y = 0; y < 240; y++) {
write_pixel(x,y,0);
}
}
}
/* write a single character to the character buffer at x,y
* x in [0,79], y in [0,59]
*/
void write_char(int x, int y, char c) {
// VGA character buffer
volatile char * character_buffer = (char *) (0x09000000 + (y<<7) + x);
*character_buffer = c;
}
void write_line(char * str, int x,int y)
{
int i;
for(i=0; i<strlen(str);i++)
{
write_char(x+i,y,str[i]);
}
}
void write_tomainscore(int score)
{
write_score(20,52,score);
}
void write_score(int x, int y, int score)
{
char string[4];
sprintf(string,"%d",score);
write_line(string,x,y);
}
void draw_linehorz(int x, int y,int length, short colour)
{
int i=0;
for(i=0; i<length; i++)
{
write_pixel(x+i,y,colour);
}
}
void draw_linevert(int x, int y,int length, short colour)
{
int i=0;
for(i=0; i<length; i++)
{
write_pixel(x,y+i,colour);
}
}
void initialization()
{
int horizontal_offset=0;
int vert_offset=130;
clear_screen();
//write_line("Score:",1,2+50);
//write_line("Round:",1,3+50);
draw_linehorz(horizontal_offset,16+vert_offset,300,0xf800);
draw_linehorz(horizontal_offset,32+vert_offset,300,0xf800);
int i;
for(i=0; i<=300; i=i+30)
{
draw_linevert(i+horizontal_offset,16+vert_offset,35,0xf800);
}
// /****VARIABLES*********/
// FILE * bmpfile;
// Colours ** pixeldata;
// BITMAPINFOHEADER info;
// BITMAPFILEHEADER head;
// //check if file exists
// bmpfile= fopen("s.bmp","r+b");
// if(!bmpfile)
// {
// printf("Bmp File Does Not Exist. ");
// return 0;
// }
// // find starting point
// fseek(bmpfile, 0,0);
// // determine if valid bmp and 16-bits
// validBMP(bmpfile);
// // retrieve dimensions info from header
// info=retrieveInfo(bmpfile);
// // set global variables to make available in all functions
// height= info.biHeight;
// width= info.biWidth;
// pixeldata=createMatrix();
// //loadImage(bmpfile,pixeldata);
// int k,j;
// Colours tmp;
// //offset position
// long pos = 52;
// fseek(bmpfile,0,0);
// for (j=0; i<height; i++){
// for (k=0; j<width; j++){
// pos+= 2;
// fseek(bmpfile,pos,0);
// fread(&tmp,(sizeof(Colours)),1,bmpfile);
// pixeldata[j][k] = tmp;
// }
// }
// write_pixel(25,25,pixeldata[4][4].colour);
// drawbackgroundimage(pixeldata);
drawArrayofBoxes((endofscreenx-20)/2,40,16,0xf800);
//updatepins(10,2);
//player 1
drawfilledBox(0,0,50,10,0xf800);
//player 2
//write_line("Player 1",0,0);
//write_line("Player 2",pixelbufferx-50/4,0);
}
Colours ** createMatrix()
{
Colours** Matrix=(Colours**)malloc(sizeof(Colours*)*height);
//for loop ctr
int i;
for(i=0; i<height; i++)
{
Matrix[i]= (Colours *)malloc(sizeof(Colours*)*width);
}
return Matrix;
}
//BITMAPINFOHEADER retrieveInfo(FILE * file)
//{
// BITMAPINFOHEADER info;
// // read image width
// fseek(file, 18, 0);
//
// fread(&info.biWidth,1,4,file);
// //printf("%d\n", info.biWidth);
//
// // read image height
// fseek(file,22, 0);
//
//
//
// fread(&info.biHeight,1,4,file);
// // printf("%d\n", info.biHeight);
//
// // Image size in bytes
// fseek(file,34,0);
// fread(&info.biSizeImage,1,4,file);
// printf("%d\n", info.biSizeImage);
//
// return info;
//
//}
//void validBMP(FILE * file)
//{
//
// char type[3];
// unsigned short bit;
// //assign starting position to check HEADER
// fseek(file,0,0);
//
// //read first two bytes
// fread(type, 1,2, file);
//
// //since fread does not insert null terminating character, must insert manually
//
// type[2]='\0';
// //
// if (strcmp(type,"BM")==0)
// {
// printf("The file is BMP format\n");
//
// }
// else
// {
//
//
// printf("Error, not a BMP");
// exit(0);
// }
//
//
//
//// 28 bytes offset
// fseek(file,28,0);
// fread(&bit,1,2, file);
// if(bit!= 16)
// {
// printf("The file is not 16-bit format \n");
// exit(0);
// }
//
//
//
//}
void drawbackgroundimage(Colours ** pixels)
{
int i,j;
int offsetx=25;
int offsety=25;
for(i=0;i<height;i++)
{
for(j=0;j<width;j++)
{
write_pixel(offsetx+j,offsety+i,pixels[i][j].colour);
}
}
}
void drawBox(int x, int y, int sidelength, short colour)
{
int i;
for(i=0; i<=sidelength; i++)
{
write_pixel(x,y+i,colour);
write_pixel(x+i,y,colour);
write_pixel(x+sidelength,y+i,colour);
write_pixel(x+i,y+sidelength,colour);
}
}
void drawArrayofBoxes(int x,int y, int sidelength,short colour)
{
int i;
//draw 1st row
for(i=0;i<4; i++)
{
drawBox(x+sidelength*i,y,sidelength,colour);
}
// draw 2nd row
int offset;
offset=(4*sidelength-3*sidelength)/2;
for(i=0;i<3;i++)
{
drawBox(x+i*sidelength+offset,y+sidelength,sidelength,colour);
}
offset=offset+(3*sidelength-2*sidelength)/2;
for(i=0;i<2;i++)
{
drawBox(x+i*sidelength+offset,y+sidelength*2,sidelength,colour);
}
offset=offset+(2*sidelength-sidelength)/2;
for(i=0;i<1;i++)
{
drawBox(x+i*sidelength+offset,y+sidelength*3,sidelength,colour);
}
}
void drawX(int x, int y, int side, short colour)
{
int i;
for(i=0; i<=side; i++)
{
write_pixel(x+i,y+i,colour);//makes backslash shape
write_pixel(x+i,y+side-i, colour);//makes slash shape
}
}
void updatepins(int score, int round)
{
int sizeofBox=16;
int sizeofXs=5;
int rowoffset2=(4*sizeofBox-3*sizeofBox)/2;
int rowoffset3=rowoffset2+(3*sizeofBox-2*sizeofBox)/2;
int rowoffset4=rowoffset3+(2*sizeofBox-sizeofBox)/2;
int boxnum;
int horzposition=(endofscreenx-20)/2;
int offset;
offset=(sizeofBox-sizeofXs)/2;
drawX(horzposition+offset, 40+offset,5,0);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0);
drawX(horzposition+offset+sizeofBox*3, 40+offset,5,0);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0);
drawX(horzposition+offset+rowoffset2+sizeofBox*2,40+offset+sizeofBox,5,0);
drawX(horzposition+offset+rowoffset3, 40+offset+sizeofBox*2,5,0);
drawX(horzposition+offset+rowoffset3+sizeofBox, 40+offset+sizeofBox*2,5,0);
drawX(horzposition+offset+rowoffset4,40+offset+sizeofBox*3,5,0);
//drawArrayofBoxes((endofscreenx-20)/2,40,16,0xf800);
//erase and redraw
//if score
if(score==1)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
}
if(score==2)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
}
if(score==3)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
}
if(score==4)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
}
if(score==5)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
}
if(score==6)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset3, 40+offset+sizeofBox*2,5,0XFFFF);
}
if(score==7)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset3, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset3+sizeofBox, 40+offset+sizeofBox*2,5,0XFFFF);
}
if(score==8)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset3, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset3+sizeofBox, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset4,40+offset+sizeofBox*3,5,0XFFFF);
}
if(score==9)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox*2,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset3, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset3+sizeofBox, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset4,40+offset+sizeofBox*3,5,0XFFFF);
}
if(score==10)
{
drawX(horzposition+offset, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*2, 40+offset,5,0XFFFF);
drawX(horzposition+offset+sizeofBox*3, 40+offset,5,0XFFFF);
drawX(horzposition+offset+rowoffset2,40+offset+sizeofBox,5,0XFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset2+sizeofBox*2,40+offset+sizeofBox,5,0xFFFF);
drawX(horzposition+offset+rowoffset3, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset3+sizeofBox, 40+offset+sizeofBox*2,5,0XFFFF);
drawX(horzposition+offset+rowoffset4,40+offset+sizeofBox*3,5,0XFFFF);
}
// print score in location: round*offset
// make pattern 1
//if score==2
// make pattern 2
//if score==3
// make pattern 3
//if score==5
// make pattern 4
//if score==8
// make pattern 5
//if score==9
// make pattern 6
//if score==10
// make pattern strike
}
void drawfilledBox(int x, int y, int lengthx, int lengthy, short colour)
{
int i,j;
for( i=0; i<lengthy; i++)
{
for(j=0; j<lengthx;j++)
{
write_pixel(x+j,y+i,colour);
}
}
}
void fillinscore(int score, int round, int player)
{
int columnwidth=30;
int columnlength=4;
round = round/2;
round++;
if(player==0)
{
int offset=1;
int roundoffset=(round-1)*30/4;
write_score(roundoffset+offset,(146)/4+offset,score);
}
else if(player==1)
{
int offset=1;
int roundoffset=(round-1)*30/4;
write_score(roundoffset+offset,146/4+offset+columnlength,score);
}
updatepins(score,round);
}
void blink(int player)
{
if(player == 1)
{
drawfilledBox(0,0,50,10,0xf800);
write_line("Player 1", 1,1);
}
else if (player == 0)
{
drawfilledBox(0,0,50,10,0x000f);
write_line("Player 2", 1,1);
}
else if (player == 2)
{
drawfilledBox(0,0,50,10,0x00ff);
write_line("GAMEOVER", 1,1);
}
}