/*从比特流中读取N比特 */ unsigned long getbits(Bit_stream_struc *bs, int N) { unsigned long val=0; register int i; register int j = N; register int k, tmp; if (N > MAX_LENGTH) printf("Cannot read or write more than %d bits at a time.\n", MAX_LENGTH); bs->totbit += N; while (j > 0) { if (!bs->buf_bit_idx) { bs->buf_bit_idx = 8; bs->buf_byte_idx--; if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) { if (bs->eob) bs->eobs = TRUE; else { for (i=bs->buf_byte_idx; i>=0;i--) bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i]; refill_buffer(bs); bs->buf_byte_idx = bs->buf_size-1; } } } k = MIN(j, bs->buf_bit_idx); tmp = bs->buf[bs->buf_byte_idx]&putmask[bs->buf_bit_idx]; tmp = tmp >> (bs->buf_bit_idx-k); val |= tmp << (j-k); bs->buf_bit_idx -= k; j -= k; } return val; }
/* Called only from hread(); when called, our buffer is empty and nread bytes have already been placed in the destination buffer. */ ssize_t hread2(hFILE *fp, void *destv, size_t nbytes, size_t nread) { const size_t capacity = fp->limit - fp->buffer; char *dest = (char *) destv; dest += nread, nbytes -= nread; // Read large requests directly into the destination buffer while (nbytes * 2 >= capacity && !fp->at_eof) { ssize_t n = fp->backend->read(fp, dest, nbytes); if (n < 0) { fp->has_errno = errno; return n; } else if (n == 0) fp->at_eof = 1; fp->offset += n; dest += n, nbytes -= n; nread += n; } while (nbytes > 0 && !fp->at_eof) { size_t n; ssize_t ret = refill_buffer(fp); if (ret < 0) return ret; n = fp->end - fp->begin; if (n > nbytes) n = nbytes; memcpy(dest, fp->begin, n); fp->begin += n; dest += n, nbytes -= n; nread += n; } return nread; }
size_t BufferedFile::get_bytes(address buffer, jint num, bool is_buffered) { jint total_read = 0; Buffer::Raw fb = data_buffer(); if (!is_buffered) { OsFile_seek(file_pointer(), (long)(-(count() - index())), SEEK_CUR); set_count(0); // flush buffer set_index(0); return OsFile_read(file_pointer(), buffer, 1, num); } else { while (!at_eof() && num > 0) { int num_to_read = (num > (count() - index())) ? (count() - index()) :num; // num_to_read may be zero if (num_to_read) { jvm_memcpy(buffer, fb().base_address() + (index() * sizeof (jbyte)), num_to_read); buffer += num_to_read; num -= num_to_read; set_index(index() + num_to_read); total_read += num_to_read; } if (num > 0) { refill_buffer(); } } } return total_read; }
/*从比特流中读取1比特*/ unsigned int get1bit(Bit_stream_struc *bs) { unsigned int bit; register int i; bs->totbit++; if (!bs->buf_bit_idx) { bs->buf_bit_idx = 8; bs->buf_byte_idx--; if ((bs->buf_byte_idx < MINIMUM) || (bs->buf_byte_idx < bs->eob)) { if (bs->eob) bs->eobs = TRUE; else { for (i=bs->buf_byte_idx; i>=0;i--) bs->buf[bs->buf_size-1-bs->buf_byte_idx+i] = bs->buf[i]; refill_buffer(bs); bs->buf_byte_idx = bs->buf_size-1; } } } bit = bs->buf[bs->buf_byte_idx]&mask[bs->buf_bit_idx-1]; bit = bit >> (bs->buf_bit_idx-1); bs->buf_bit_idx--; return(bit); }
/*read 1 bit from the bit stream */ unsigned int get1bit() { unsigned int bit; if (eobs) return 0; bit = (bfr[byteidx] & masks[bitidx - 1]) >> (bitidx - 1); totbits++; bitidx--; if (!bitidx) { bitidx = 8; byteidx++; if (byteidx == bufcount) { if (bufcount == BUFFER_SIZE) refill_buffer(); else eobs = true; byteidx = 0; } } return bit; }
/*read N bit from the bit stream */ unsigned int getbits(int N) { unsigned int val = 0; int i = N; unsigned int j; // Optimize: we are on byte boundary and want to read multiple of bytes! if ((bitidx == 8) && ((N & 7) == 0)) { i = N >> 3; while (i > 0) { val = (val << 8) | bfr[byteidx]; byteidx++; totbits += 8; if (byteidx == bufcount) { if (bufcount == BUFFER_SIZE) refill_buffer(); else eobs = true; byteidx = 0; } i--; } return val; }
static int read_line(CvsServerCtx * ctx, char * p, int maxlen) { int len = 0; if (maxlen <= 0) return -1; while (1) { if (ctx->head == ctx->tail) if (refill_buffer(ctx) <= 0) return -1; /* break out without advancing head if buffer is exhausted */ if (maxlen == 1 || (*p = *ctx->head++) == '\n') { *p = 0; break; } p++; len++; maxlen--; } return len; }
int BitReader::seek_bits(uint64_t nbits){ /* FIXME: Logic to check if we overrun the buffer ... */ if (nbits && (bytes_left() == 0)) refill_buffer(); int bitpr = _bitp % 8; _bitp += nbits; _curr_byte += ((bitpr + nbits) / 8 - get_current_byte()); (*_curr_byte) <<= (_bitp % 8); return 1; }
ssize_t hpeek(hFILE *fp, void *buffer, size_t nbytes) { size_t n = fp->end - fp->begin; while (n < nbytes) { ssize_t ret = refill_buffer(fp); if (ret < 0) return ret; else if (ret == 0) break; else n += ret; } if (n > nbytes) n = nbytes; memcpy(buffer, fp->begin, n); return n; }
ssize_t hgetdelim(char *buffer, size_t size, int delim, hFILE *fp) { char *found; size_t n, copied = 0; ssize_t got; if (size < 1 || size > SSIZE_MAX) { fp->has_errno = errno = EINVAL; return -1; } if (writebuffer_is_nonempty(fp)) { fp->has_errno = errno = EBADF; return -1; } --size; /* to allow space for the NUL terminator */ do { n = fp->end - fp->begin; if (n > size - copied) n = size - copied; /* Look in the hFILE buffer for the delimiter */ found = memchr(fp->begin, delim, n); if (found != NULL) { n = found - fp->begin + 1; memcpy(buffer + copied, fp->begin, n); buffer[n + copied] = '\0'; fp->begin += n; return n + copied; } /* No delimiter yet, copy as much as we can and refill if necessary */ memcpy(buffer + copied, fp->begin, n); fp->begin += n; copied += n; if (copied == size) { /* Output buffer full */ buffer[copied] = '\0'; return copied; } got = refill_buffer(fp); } while (got > 0); if (got < 0) return -1; /* Error on refill. */ buffer[copied] = '\0'; /* EOF, return anything that was copied. */ return copied; }
static size_t input_despotify_read(struct input_stream *is, void *ptr, size_t size, G_GNUC_UNUSED GError **error_r) { struct input_despotify *ctx = (struct input_despotify *)is; size_t to_cpy = size; if (ctx->len_available == 0) refill_buffer(ctx); if (ctx->len_available < size) to_cpy = ctx->len_available; memcpy(ptr, ctx->pcm.buf, to_cpy); ctx->len_available -= to_cpy; is->offset += to_cpy; return to_cpy; }
/* Called only from hread(); when called, our buffer is empty and nread bytes have already been placed in the destination buffer. */ ssize_t hread2(hFILE *fp, void *destv, size_t nbytes, size_t nread) { const size_t capacity = fp->limit - fp->buffer; int buffer_invalidated = 0; char *dest = (char *) destv; dest += nread, nbytes -= nread; // Read large requests directly into the destination buffer while (nbytes * 2 >= capacity && !fp->at_eof) { ssize_t n = fp->backend->read(fp, dest, nbytes); if (n < 0) { fp->has_errno = errno; return n; } else if (n == 0) fp->at_eof = 1; else buffer_invalidated = 1; fp->offset += n; dest += n, nbytes -= n; nread += n; } if (buffer_invalidated) { // Our unread buffer is empty, so begin == end, but our already-read // buffer [buffer,begin) is likely non-empty and is no longer valid as // its contents are no longer adjacent to the file position indicator. // Discard it so that hseek() can't try to take advantage of it. fp->offset += fp->begin - fp->buffer; fp->begin = fp->end = fp->buffer; } while (nbytes > 0 && !fp->at_eof) { size_t n; ssize_t ret = refill_buffer(fp); if (ret < 0) return ret; n = fp->end - fp->begin; if (n > nbytes) n = nbytes; memcpy(dest, fp->begin, n); fp->begin += n; dest += n, nbytes -= n; nread += n; } return nread; }
/* open the device to read the bit stream from it */ void init_getbits(char *bs_filename) { if ((bitfile = fopen(bs_filename, "rb")) == NULL) { printf("Unable to open file %s for reading.\n", bs_filename); exit(1); } byteidx = 0; bitidx = 8; totbits = 0.0; bufcount = 0; eobs = false; if (!refill_buffer()) { if (eobs) { printf("Unable to read from file %s.\n", bs_filename); exit(1); } } }
static void parse_header(int fd, struct http_data *data) { struct http_callbacks callbacks = { .data = data, .request_method = http_request_method, .request_uri = http_request_uri, .http_version = http_request_version, .http_field = http_field, .header_done = http_request_done }; struct sock sock = { .fd = fd }; http_parser_init(&parser, &sock); do { refill_buffer(&sock); /* TODO: parser and sock object should be rolled together */ ssize_t nbytes_p = http_parser_execute(&parser, &sock, &callbacks); sock.pos += nbytes_p; } while (!http_parser_is_finished(&sock)); }
/* Called only from hgetc(), when our buffer is empty. */ int hgetc2(hFILE *fp) { return (refill_buffer(fp) > 0)? (unsigned char) *(fp->begin++) : EOF; }
static int64_t peek_read(io_t *io, void *buffer, int64_t len) { int64_t ret = 0; /* Have we previously encountered an error? */ if (DATA(io)->length < 0) { return DATA(io)->length; } /* Is some of this data in the buffer? */ if (DATA(io)->buffer && DATA(io)->length) { ret = MIN(len,DATA(io)->length - DATA(io)->offset); /* Copy anything we've got into their buffer, and shift our * offset so that we don't peek at the data we've read again */ memcpy(buffer, DATA(io)->buffer + DATA(io)->offset, ret); buffer += ret; DATA(io)->offset += ret; len -= ret; } /* Use the child reader to get the rest of the required data */ if (len>0) { /* To get here, the buffer must be empty */ assert(DATA(io)->length-DATA(io)->offset == 0); int64_t bytes_read; /* If they're reading exactly a block size, just use that, no point in malloc'ing * and memcpy()ing needlessly. However, if the buffer isn't aligned, we need to * pass on an aligning buffer, skip this and do it into our own aligned buffer. */ if ((len % MIN_READ_SIZE == 0) && ((ptrdiff_t)buffer % 4096)==0) { assert(((ptrdiff_t)buffer % 4096) == 0); bytes_read = DATA(io)->child->source->read( DATA(io)->child, buffer, len); /* Error? */ if (bytes_read < 1) { /* Return if we have managed to get some data ok */ if (ret > 0) return ret; /* Return the error upstream */ return bytes_read; } } else { bytes_read = refill_buffer(io, len); if (bytes_read < 1) { /* Return if we have managed to get some data ok */ if (ret > 0) return ret; /* Return the error upstream */ return bytes_read; } /* Now grab the number of bytes asked for. */ len = len < bytes_read ? len : bytes_read; memcpy(buffer, DATA(io)->buffer, len); DATA(io)->offset = len; bytes_read = len; } ret += bytes_read; } /* Have we read past the end of the buffer? */ if (DATA(io)->buffer && DATA(io)->offset >= DATA(io)->length) { /* If so, free the memory it used */ free(DATA(io)->buffer); DATA(io)->buffer = NULL; DATA(io)->offset = 0; DATA(io)->length = 0; } return ret; }
int main(){ printf("NEW GAME!\n\n"); volatile int old_game_state = 0; int doge = 0; //animation counter //buffer init stuff pixel_buffer = alt_up_pixel_buffer_dma_open_dev("/dev/pixel_buffer_dma"); char_buffer = alt_up_char_buffer_open_dev("/dev/char_drawer"); //phil's stuff master* m = master_init(); printf("NEW GAME!\n\n"); // Set the 1st buffer address alt_up_pixel_buffer_dma_change_back_buffer_address(pixel_buffer,pixel_buffer_addr1); // Swap buffers – we have to swap because there is only an API function // to set the address of the background buffer. alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while (alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)); // Set the 2nd buffer address alt_up_pixel_buffer_dma_change_back_buffer_address(pixel_buffer,pixel_buffer_addr2); printf("NEW GAME!\n\n"); // Clear the screen alt_up_pixel_buffer_dma_clear_screen(pixel_buffer, 1); alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while (alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)); alt_up_pixel_buffer_dma_clear_screen(pixel_buffer, 1); alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while (alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)); alt_up_char_buffer_clear(char_buffer); //Write some text alt_up_char_buffer_string(char_buffer, "LOADING...", 0, 0); //load bitmap files title = read_bmp("title.bmp"); alt_up_char_buffer_string(char_buffer, "title.bmp", 0, 2); menu = read_bmp("menu.bmp"); alt_up_char_buffer_string(char_buffer, "MENU.BMP", 0, 3); selA = read_bmp("selA.bmp"); alt_up_char_buffer_string(char_buffer, "selA.bmp", 0, 4); selB = read_bmp("selB.bmp"); alt_up_char_buffer_string(char_buffer, "selB.bmp", 0, 5); selC = read_bmp("selC.bmp"); alt_up_char_buffer_string(char_buffer, "selC.bmp", 0, 6); dead = read_bmp("dead.bmp"); alt_up_char_buffer_string(char_buffer, "dead.bmp", 0, 7); bmp * b = read_bmp("para1.bmp"); alt_up_char_buffer_string(char_buffer, "para.bmp", 0, 8); bmp * doge0 = read_bmp("doge0.bmp"); alt_up_char_buffer_string(char_buffer, "doge0.bmp", 0, 9); bmp * doge1 = read_bmp("doge1.bmp"); alt_up_char_buffer_string(char_buffer, "doge1.bmp", 0, 10); bmp * doge2 = read_bmp("doge2.bmp"); alt_up_char_buffer_string(char_buffer, "doge2.bmp", 0, 11); bmp * doge3 = read_bmp("doge3.bmp"); alt_up_char_buffer_string(char_buffer, "doge3.bmp", 0, 12); bmp * flat = read_bmp("flat.bmp"); alt_up_char_buffer_string(char_buffer, "flat.bmp", 0, 13); bmp * coin = read_bmp("coin.bmp"); alt_up_char_buffer_string(char_buffer, "coin.bmp", 0, 14); bmp * spike = read_bmp("spike.bmp"); alt_up_char_buffer_string(char_buffer, "spike.bmp", 0, 15); bmp * box1 = read_bmp("box1.bmp"); alt_up_char_buffer_string(char_buffer, "box1.bmp", 0, 16); bmp * box3 = read_bmp("box3.bmp"); alt_up_char_buffer_string(char_buffer, "box3.bmp", 0, 17); bmp * low = read_bmp("low.bmp"); alt_up_char_buffer_string(char_buffer, "low.bmp", 0, 18); bmp * flatb = read_bmp("flatb.bmp"); alt_up_char_buffer_string(char_buffer, "flatb.bmp", 0, 19); bmp * flatr = read_bmp("flatr.bmp"); alt_up_char_buffer_string(char_buffer, "flatr.bmp", 0, 20); bmp * blue = read_bmp("bstar.bmp"); alt_up_char_buffer_string(char_buffer, "blue.bmp", 0, 21); bmp * red = read_bmp("rstar.bmp"); alt_up_char_buffer_string(char_buffer, "red.bmp", 0, 22); bmp * flag_img = read_bmp("flag.bmp"); alt_up_char_buffer_string(char_buffer, "flag.bmp", 0, 23); name = read_bmp("name.bmp"); alt_up_char_buffer_string(char_buffer, "name.bmp", 0, 24); instr = read_bmp("instr.bmp"); alt_up_char_buffer_string(char_buffer, "instr.bmp", 0, 25); dcol = read_bmp("dcol.bmp"); alt_up_char_buffer_string(char_buffer, "dcol.bmp", 0, 26); win = read_bmp("win.bmp"); alt_up_char_buffer_string(char_buffer, "win.bmp", 0,27); alt_up_char_buffer_clear(char_buffer); printf("NEW GAME!\n\n"); //interrupt init stuff (for object writing) //TIMERPERIOD int timer_period = 1 * 500000; IOWR_16DIRECT(TIMER_0_BASE, 8, timer_period & 0xFFFF); //writes the period to the hardware timer IOWR_16DIRECT(TIMER_0_BASE, 12, timer_period >> 16); IOWR_16DIRECT(TIMER_0_BASE, 4, 1 << 3); //stop timer alt_irq_register(TIMER_0_IRQ,NULL,(void*)handle_timer_interrupts);//registers function to a specific IRQ //IOWR_16DIRECT(TIMER_0_BASE, 4, 0x5); //start timer //SET UP KEYBOARD INTERRUPT// ps2 = alt_up_ps2_open_dev(KEYBOARD_NAME); alt_up_ps2_init(ps2); alt_up_ps2_clear_fifo(ps2); //void* keyboard_control_register_ptr = (void*) (PS2_0_BASE + 4); alt_irq_register(PS2_0_IRQ, m, keyboard_ISR); alt_up_ps2_enable_read_interrupt(ps2); char sw = 0; char p_sw = 0; ///////////////////////////////////////////////////////////////////////// printf("NEW GAME!\n\n"); //SUPERDUPERLOOP while (1){ printf("old state:%i\nnew state: %i\n\n",old_game_state, game_state); draw_menu(game_state); //update screen while (old_game_state == game_state); printf("old state:%i\nnew state: %i\n\n",old_game_state, game_state); //only when entering a new menu alt_up_char_buffer_clear(char_buffer); //ENTER GAME LOOP if (game_state == 5){ printf("START GAME! LEVEL: %i\n\n", highlighted_level); alt_up_char_buffer_string(char_buffer, playername, 10, 4); if (highlighted_level == 1) { free_bmp(b); b = read_bmp("para1.bmp"); game_start(m,b,"lvl/1.txt","song1.wav"); } else if (highlighted_level == 2) { free_bmp(b); b = read_bmp("bg2.bmp"); game_start(m,b,"lvl/2.txt","a/abcd.wav"); } else{ free_bmp(b); b = read_bmp("bg3.bmp"); game_start(m,b,"lvl/2.txt","a/nyan1.wav"); } //collision loop while(!m->c->collide && !m->c->win){ alt_up_char_buffer_string(char_buffer, "POINTS: ", 50, 4); char str[15]; sprintf(str, "%d", m->c->points); alt_up_char_buffer_string(char_buffer, str, 58, 4); sw = IORD_8DIRECT(SWITCHES_BASE,0); IOWR_8DIRECT(LEDS_BASE,0,sw); if(sw == 1 && p_sw == 0){ //m->ab->sfx_flag = 1; m->c->jump_pressed = 1; } p_sw = sw; //boxes int i; for( i= 0 ; i < OBJECT_SIZE ; i++) { if(m->o->color[i] == -1) draw_object(pixel_buffer, box, flat, i); else if(m->o->color[i] == 0) draw_object(pixel_buffer, box, flatb, i); else if(m->o->color[i] == 1) draw_object(pixel_buffer, box, flatr, i); draw_object(pixel_buffer, co, coin, i ); draw_object(pixel_buffer, spikes, spike, i); draw_object(pixel_buffer, box_3, box3, i); draw_object(pixel_buffer, box_1, box1, i); // if(m->color_gates->color[i] == 1) // draw_object(pixel_buffer,cgates, rgate,i); // else if (m->color_gates->color[i] == 0) // draw_object(pixel_buffer,cgates, bgate,i); } //draws the win flag draw_object(pixel_buffer, flag, flag_img, 0); //Draw Doge if (m->c->ducking) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,low); else{ doge++; if(doge == 37) doge = 0; if( doge <9) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge0); else if (doge <18) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge1); else if (doge <27) draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge2); else draw_bmp(pixel_buffer,m->c->x - m->c->width, m->c->y - m->c->height,doge3); } //Draw Color Indicator if(m->c->color == 0) draw_bmp(pixel_buffer, m->c->x- m->c->width + 5, m->c->y - m->c->height - 10, blue); else draw_bmp(pixel_buffer,m->c->x- m->c->width + 5, m->c->y - m->c->height - 10, red); p_counter++; if(p_counter == 3){ p_shift++; alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer unrolled_parallax_draw(pixel_buffer, b); }else if(p_counter == 4){ //if(p_counter == 1){ alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer unrolled_parallax_draw(pixel_buffer, b); p_counter = 0; }else{ // alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); // while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) // refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer // unrolled_parallax_draw(pixel_buffer, b); alt_up_pixel_buffer_dma_swap_buffers(pixel_buffer); while(alt_up_pixel_buffer_dma_check_swap_buffers_status(pixel_buffer)) refill_buffer(m->ab, "a/abcd.wav");//refills the audio buffer int j; for( j = 0 ; j < OBJECT_SIZE ; j++) { clear_object(pixel_buffer, box, b, j); clear_object(pixel_buffer, co, b, j); clear_object(pixel_buffer, spikes, b, j); clear_object(pixel_buffer, box_3, b, j); clear_object(pixel_buffer, box_1, b, j); } clear_object(pixel_buffer,flag,b,0); //clear doge clear_doge(pixel_buffer, m->c->x - m->c->width, m->c->y - m->c->height , b); //clear_loc(pixel_buffer,m->c->x- m->c->width + 5, m->c->y - m->c->height - 10,m->c->x- m->c->width + 5 - 10, m->c->y - m->c->height - 20,b); } } alt_up_char_buffer_string(char_buffer, "POINTS: ", 50, 4); char str[15]; sprintf(str, "%d", m->c->points); alt_up_char_buffer_string(char_buffer, str, 58, 4); printf("game exited\n"); if(m->c->win) game_state = 7; else game_state = 6; highlighted_item = 3; game_reset(m); } //exit game mode, restart superduperloop in main menu old_game_state = game_state; } return 0; }