1 | #include "game.h" |
---|
2 | #include "disp.h" |
---|
3 | #include <stdio.h> |
---|
4 | #include <malloc.h> |
---|
5 | #include <user_lock.h> |
---|
6 | |
---|
7 | /////////////////////// |
---|
8 | // Global variables |
---|
9 | /////////////////////// |
---|
10 | |
---|
11 | unsigned char* buf; // one image buffer |
---|
12 | void * sts; // buffer status |
---|
13 | volatile unsigned int slice_x; // slice index (shared) |
---|
14 | volatile unsigned int slice_count; // slice count (shared) |
---|
15 | sqt_lock_t slice_get_lock; // slice index lock |
---|
16 | sqt_lock_t slice_done_lock; // slice index lock |
---|
17 | pthread_t trdid[1024]; // thread identifiers array |
---|
18 | Game game; // Game state |
---|
19 | unsigned int fbuf_x_size; // FBF width |
---|
20 | unsigned int fbuf_y_size; // FBF height |
---|
21 | |
---|
22 | // Textures |
---|
23 | unsigned char* g_tex[] = |
---|
24 | { |
---|
25 | NULL, // 0 |
---|
26 | NULL, // rock |
---|
27 | NULL, // door |
---|
28 | NULL, // handle |
---|
29 | NULL, // wood |
---|
30 | }; |
---|
31 | |
---|
32 | ////////////////////////// |
---|
33 | // Exported functions |
---|
34 | ////////////////////////// |
---|
35 | |
---|
36 | ////////////////////////////////////////// |
---|
37 | __attribute__((constructor)) void render() |
---|
38 | { |
---|
39 | unsigned int slice; |
---|
40 | |
---|
41 | while ( game.exit == 0 ) |
---|
42 | { |
---|
43 | dispRenderSlice( &slice ); |
---|
44 | } |
---|
45 | giet_pthread_exit( NULL ); |
---|
46 | } |
---|
47 | |
---|
48 | //////////////////////////////////////// |
---|
49 | __attribute__((constructor)) void main() |
---|
50 | { |
---|
51 | unsigned int w, h, p; // platform parameters |
---|
52 | |
---|
53 | unsigned int i, j, n; // indexes for loops |
---|
54 | |
---|
55 | // get private TTY |
---|
56 | giet_tty_alloc(0); |
---|
57 | |
---|
58 | giet_tty_printf("\n[RAYCAST] enters main at cycle %d\n", |
---|
59 | giet_proctime() ); |
---|
60 | |
---|
61 | // get and check platform parameters |
---|
62 | giet_procs_number(&w, &h, &p); |
---|
63 | |
---|
64 | giet_pthread_assert( (w<=16) , "[RAYCAST ERROR] check hardware config" ); |
---|
65 | giet_pthread_assert( (h<=16) , "[RAYCAST ERROR] check hardware config" ); |
---|
66 | giet_pthread_assert( (p<= 4) , "[RAYCAST ERROR] check hardware config" ); |
---|
67 | |
---|
68 | // check frame buffer availability |
---|
69 | giet_fbf_size( &fbuf_x_size , &fbuf_y_size ); |
---|
70 | giet_pthread_assert( ((fbuf_x_size) && (fbuf_y_size)) , |
---|
71 | "[RAYCAST ERROR] no frame buffer available" ); |
---|
72 | |
---|
73 | // compute total number of threads |
---|
74 | unsigned int nthreads = w * h * p; |
---|
75 | |
---|
76 | // Initialize heap for each cluster |
---|
77 | for (i = 0; i < w; i++) |
---|
78 | for (j = 0; j < h; j++) |
---|
79 | heap_init(i, j); |
---|
80 | |
---|
81 | // Initialize lock protecting slice allocator |
---|
82 | sqt_lock_init( &slice_get_lock, w , h , p ); |
---|
83 | sqt_lock_init( &slice_done_lock, w , h , p ); |
---|
84 | |
---|
85 | // Allocate buffer and status for CMA |
---|
86 | buf = malloc(fbuf_x_size * fbuf_x_size); |
---|
87 | sts = malloc(64); |
---|
88 | |
---|
89 | // Get frame buffer ownership |
---|
90 | giet_fbf_alloc(); |
---|
91 | |
---|
92 | // Get a CMA channel for one single user buffer |
---|
93 | giet_fbf_cma_alloc( 1 ); |
---|
94 | |
---|
95 | // Register the user buffer and status |
---|
96 | giet_fbf_cma_init_buf( 0 , buf , sts ); |
---|
97 | |
---|
98 | // Start Chained buffer DMA |
---|
99 | giet_fbf_cma_start(); |
---|
100 | |
---|
101 | // Load textures |
---|
102 | g_tex[1] = dispLoadTexture("misc/rock_32.raw"); |
---|
103 | g_tex[2] = dispLoadTexture("misc/door_32.raw"); |
---|
104 | g_tex[3] = dispLoadTexture("misc/handle_32.raw"); |
---|
105 | g_tex[4] = dispLoadTexture("misc/wood_32.raw"); |
---|
106 | |
---|
107 | // Initialise game |
---|
108 | gameInit(); |
---|
109 | |
---|
110 | giet_tty_printf("\n[RAYCAST] initialisation completed at cycle %d\n", |
---|
111 | giet_proctime() ); |
---|
112 | |
---|
113 | // launch other threads to speed render |
---|
114 | for ( n = 1 ; n < nthreads ; n++ ) |
---|
115 | { |
---|
116 | if ( giet_pthread_create( &trdid[n], |
---|
117 | NULL, // no attribute |
---|
118 | &render, |
---|
119 | NULL ) ) // no argument |
---|
120 | { |
---|
121 | giet_tty_printf("\n[RAYCAST ERROR] creating thread %d\n", n ); |
---|
122 | giet_pthread_exit( NULL ); |
---|
123 | } |
---|
124 | } |
---|
125 | |
---|
126 | // Game main loop : display one frame |
---|
127 | // and get one player move per iteration |
---|
128 | while ( game.exit == 0 ) |
---|
129 | { |
---|
130 | // check user buffer empty |
---|
131 | giet_fbf_cma_check( 0 ); |
---|
132 | |
---|
133 | // re-initialise synchronisation variables |
---|
134 | // to start parallel synthesis |
---|
135 | slice_count = 0; |
---|
136 | slice_x = 0; |
---|
137 | |
---|
138 | // contribute to synthesis |
---|
139 | unsigned int slice; |
---|
140 | while ( dispRenderSlice( &slice ) ); |
---|
141 | |
---|
142 | // Wait last slice completion |
---|
143 | while (slice_count < fbuf_x_size) giet_tty_printf(" "); |
---|
144 | |
---|
145 | // display image |
---|
146 | giet_fbf_cma_display( 0 ); |
---|
147 | |
---|
148 | // get new player position [x,y,dir] |
---|
149 | gameControl(); |
---|
150 | } |
---|
151 | |
---|
152 | giet_pthread_exit( NULL ); |
---|
153 | |
---|
154 | } // end main() |
---|