0 / 0 / 0
Регистрация: 14.02.2013
Сообщений: 108
|
|
1
|
Объясните код, написанный на Си и Си++
04.04.2014, 18:44. Показов 909. Ответов 0
У меня такая проблема, есть задача сделать " перехват ван эйка". Нашел уже написанную программу, способствующую в этом, времени учить язык, чтобы разобраться нету, так вот, помогите разобраться в коде, как что делается, и как это все запустить. Заранее спасибо.
файл bw.c :
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
| /*********************************************************/
/***** File: bw.c *****/
/***** Alternating black and white pixels displayed! *****/
/*********************************************************/
#include <vga.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char* argv[])
{
int i, k;
printf ("Starting...Ctrl-C out when done calibrating.\n");
sleep (4);
vga_init ();
vga_setmode (G800x600x256);
for (i=0; i<800; i++)
{
for (k=0; k<600; k++)
{
vga_setcolor (0);
vga_drawpixel (i,k);
}
}
vga_setcolor(15);
vga_drawpixel(799,599);
sleep (100000);
return(0);
} |
|
файл eckbox.c :
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
| /*****************************/
/***** File: eckbox.c *****/
/***** Main eckbox file! *****/
/*****************************/
#include <vga.h>
#include <stdio.h>
#include <sys/perm.h>
#include <math.h>
#define extern static
#include <asm/io.h>
#undef extern
#ifndef ECKBOX_H
#include "eckbox.h"
#endif
#ifndef ECKBOX_IO_H
#include "io.h"
#endif
#ifndef ECKBOX_TIMING_H
#include "timing.h"
#endif
int main (int arc, char** argv)
{
/***** variables! *****/
int pix, href, vref, thresh, pixel_color, k, i;
/***** start graphics stuff *****/
vga_init();
vga_setmode(G800x600x256);
/***** get timing stuff *****/
timing_init ();
pix=timing_get_pix ();
href=timing_get_href ();
vref=timing_get_vref ();
/***** IO init for eckbox device *****/
ioperm (0x378, 3, 1); /* Get permissions for parallel port. Must be root! */
io_set_bidirectional (0x378); /* sets parallel port bidirectional: soon to be obsolete as USB or FireWire will be used */
/***** Eckbox device init *****/
eckbox_init ();
/***** Main program loop! *****/
while(1)
{
for(k=0; k<600; k++) /* 800 pixels is one row! */
{
for(i=0; i<800; i++)
{
pixel_color=eckbox_get_pixel_color ();
timing_sleep (pix);
vga_setcolor (pixel_color);
vga_drawpixel (i, k);
}
timing_sleep (href);
}
timing_sleep (vref);
}
return 0;
} |
|
файл eckbox.h :
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
| /*****************************/
/***** File: eckbox.h *****/
/***** Eckbox functions! *****/
/*****************************/
#include "io.h"
#include "timing.h"
#define ECKBOX_H
/***** Function: eckbox_init *****/
/***** initializes eckbox device! *****/
/***** Simply makes sure next pixel is 0,0 *****/
void eckbox_init ()
{
vga_setcolor(5);
vga_drawpixel(1,1);
sleep(2);
int b1=inb(0x378);
while(b1>=(inb(0x378)+20))
{
asm("nop");
}
timing_sleep(timing_get_vref());
}
/***** Function: eckbox_get_pixel_color *****/
/***** gets color of current pixel *****/
/***** With svgalib, could result *****/
/***** in some funky colors *****/
int eckbox_get_pixel_color ()
{
int b=inb(0x378);
return b;
} |
|
файл io.h
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
| /************************************/
/***** File: io.h *****/
/***** Eckbox dev io functions! *****/
/************************************/
#include <sys/perm.h>
#define extern static
#include <asm/io.h>
#undef extern
#define ECKBOX_IO_H
/***** Function: io_set_bidirectional (int) *****/
/***** Sets port iobase bidirectional! *****/
/***** Contributed by Mariuz Gumowski! *****/
int io_set_bidirectional (unsigned int iobase)
{
outb (0x66, iobase);
(void) inb (iobase); /* wait a little */
if (inb(iobase)!=0x66)
return 0; /* already in bidirectional mode */
outb (0xBB, iobase); /* one more test */
(void) inb (iobase);
if(inb(iobase)!=0xBB)
return 0; /* sure it's in bidirectional mode */
if(ioperm(iobase+2,1,1))
return 1; /* don't have the permission, so exit: failed */
outb (iobase+2, 48); /* set the bidirectional mode */
return 0;
} |
|
файл timing.h :
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
| /*****************************/
/***** File: timing.h *****/
/***** Timing functions! *****/
/*****************************/
#include <unistd.h>
#define ECKBOX_TIMING_H
/***** Global variables! *****/
unsigned long long int timing_constant;
/***** Function: rdtsc () *****/
/***** Returns num of CPU cycles since startup *****/
unsigned long long int rdtsc ()
{
unsigned long long int x;
asm volatile (".byte 0x0f, 0x31" : "=A" (x));
return x;
}
/***** Function: timing_cpu_clockspeed *****/
/***** Gets CPU clockspeed *****/
unsigned long long int timing_cpu_clockspeed ()
{
unsigned long long int t1, t2;
t1=rdtsc ();
sleep (1);
t2=rdtsc ();
return t2-t1;
}
/***** Function: timing_sleep (int) *****/
/***** Sleeps x number of CPU cycles *****/
/***** See timing_init at the bottom! *****/
void timing_sleep (int x)
{
int i;
if(timing_constant)
{
for (i=0; i<x/timing_constant; i++)
asm ("nop");
}
else
{
for (i=0; i<x; i++)
asm ("nop");
}
}
/***** Function: timing_get_pix *****/
/***** Returns pixel delay time *****/
unsigned long long int timing_get_pix ()
{
return (timing_cpu_clockspeed()/(800*600*60));
}
/***** Function: timing_get_href *****/
/***** Returns horizontal refresh *****/
unsigned long long int timing_get_href ()
{
return (timing_cpu_clockspeed()/(600*60));
}
/***** Function: timing_get_vref *****/
/***** Returns vertical refresh *****/
unsigned long long int timing_get_vref ()
{
return (timing_cpu_clockspeed()/60);
}
/***** Function: timing_init *****/
/***** Gets timing_constant, the num of cpu *****/
/***** cycles wasted in timing_sleep *****/
int timing_init ()
{
unsigned long long int t1, t2;
t1=rdtsc();
timing_sleep(2);
t2=rdtsc();
t1+=2; /* add two to t1, to account for sleep(2) */
t2-=t1; /* gets total wasted cycles */
t2/=2; /* divide by two, because we slept twice! */
timing_constant=t2;
return 0;
} |
|
0
|