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