b8

A x86 32bits native fantazy console.
git clone git://git.vgx.fr/b8
Log | Files | Refs

b8.c (1053B)


      1 #include <sys/mman.h>
      2 #include <sys/stat.h>
      3 #include <fcntl.h>
      4 #include <unistd.h>
      5 #include <stdlib.h>
      6 #include <stdio.h>
      7 
      8 #include "mem.h"
      9 #include "media.h"
     10 
     11 #define CHECK_STD_ERROR(predicate, name) do{\
     12 	if(predicate){\
     13 		perror(name);\
     14 		exit(1);\
     15 	}\
     16 }while(0)
     17 
     18 unsigned char mem[MEM_SIZE] __attribute__ ((section ("mem_section"))) = {0};
     19 
     20 void load_cart(const char *filename){
     21 	int fd = open(filename, O_RDONLY);
     22 	CHECK_STD_ERROR(fd == -1, "open");
     23 
     24 	int ret = mprotect(&mem, MEM_SIZE, PROT_READ|PROT_WRITE|PROT_EXEC);
     25 	CHECK_STD_ERROR(ret, "mprotect");
     26 
     27 	int rdsize = read(fd, mem, CART_SIZE);
     28 	CHECK_STD_ERROR(rdsize == -1, "read");
     29 }
     30 
     31 int main(int argc, char *argv[]){
     32 	void (*cart_fun)(void) = NULL;
     33 
     34 	if(argc < 2) {
     35 		fprintf(stderr, "Usage: %s file.bin\n", argv[0]);
     36 		return 1;
     37 	}
     38 
     39 	load_cart(argv[1]);
     40 
     41 	cart_fun = (void*)mem+CART_HEADER_SIZE;
     42 
     43 	load_media(argv[1]);
     44 
     45 	int cont = 1;
     46 	while(cont){
     47 		cont = update_input();
     48 
     49 		cart_fun();
     50 
     51 		update_graphics();
     52 	}
     53 
     54 	unload_media();
     55 
     56 	printf("%d\n",(int)mem[SCREEN_MEM_OFFSET]);
     57 	return 0;
     58 }