65sdl: SDL implementation, sdl: migrated into separate unit

This commit is contained in:
cr0sd 2020-03-11 15:46:53 -05:00
parent 227950c88c
commit 960dc4fe37
5 changed files with 151 additions and 113 deletions

81
65sdl.c Normal file
View File

@ -0,0 +1,81 @@
#include"65.h"
#include"cpu.h"
#include"ram.h"
#include"rom.h"
#include"joy.h"
#include"sdl.h"
#include<SDL2/SDL.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
int main(int argc,char**argv)
{
cpu_t*cpu=cpu_init();
rom_t*rom=new(rom_t);
ram_t*ram=ram_init();
joy_t*joy=joy_init(JOYPREG,2);
sdl_t*sdl=new(sdl_t);
sdl->ram=ram;
sdl->joy=joy;
pthread_t sdl_th;
// Parse command line arguments
if(argc>1)
{
rom_load_file(rom,argv[1]);
rom_map(rom,ram,PRGROM);
SDL_SetWindowTitle(sdl->win,argv[1]);
}
// Verify ROM is loaded
if(rom->rom)
cpu->pc=PRGROM;
else
puts("Failed to load ROM");
if(!ram || !rom || !joy)puts("error"),exit(1);
pthread_create(&sdl_th,NULL,sdl_thread,sdl);
// Main loop
for(;;)
{
joy_update_ram(joy,ram);
cpu_exec(cpu,ram);
if(sdl->halt) goto quit;
}
quit:
sdl->halt=1;
pthread_join(sdl_th,NULL);
//pthread_cancel(sdl_th);
free(cpu);
rom_del(rom);
free(rom);
ram_del(ram);
free(ram);
joy_del(joy);
free(joy);
sdl_del(sdl);
free(sdl);
}
// Print formatted error message
void puterr(const char*fmt,...)
{
va_list list;
va_start(list,fmt);
mvclr(STATUSLINE,0);
mvprintw(STATUSLINE,0,PROG": ");
attron(COLOR_PAIR(2));
printw("Error: ");
attron(COLOR_PAIR(1));
vw_printw(win,fmt,list);
va_end(list);
}

View File

@ -9,13 +9,13 @@ OBJS = cpu.o ram.o rom.o disasm.o joy.o vis.o
$(PROG): $(OBJS) $(PROG).o
@echo "CC $@"
@$(CC) $@.o $(OBJS) -o $@ $(CFLAGS) $(LDFLAGS)
all: $(PROG) test 65v sdl
all: $(PROG) test 65v 65sdl
65v: $(OBJS) 65v.o
@echo "CC $@"
@$(CC) $@.o $(OBJS) -o $@ $(CFLAGS) $(LDFLAGS)
sdl: $(OBJS) sdl.o
65sdl: $(OBJS) sdl.o 65sdl.o
@echo "CC $@"
@$(CC) sdl.o $(OBJS) -o $@ $(CFLAGS) $(LDFLAGS) $(SDL2_LDFLAGS) $(SDL2_CFLAGS)
@$(CC) 65sdl.o sdl.o $(OBJS) -o $@ $(CFLAGS) $(LDFLAGS) $(SDL2_LDFLAGS) $(SDL2_CFLAGS)
test:
@make -C test
.c.o:
@ -23,8 +23,8 @@ test:
@$(CC) -c $*.c $(CFLAGS) $(LDFLAGS)
clean:
@echo "RM $(PROG) 65v *.o *.nes"
@$(RM) *.o $(PROG) 65v sdl *.nes
@$(RM) *.o $(PROG) 65v 65sdl *.nes
@echo "RM test/*.nes"
@$(RM) test/*.nes
@$(RM) test/*.nes test/config
.SUFFIXES: .nes .o65 .a65 .c .o
.PHONY: all clean test

142
sdl.c
View File

@ -1,34 +1,5 @@
#include"65.h"
#include"cpu.h"
#include"ram.h"
#include"rom.h"
#include"joy.h"
#include<SDL2/SDL.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
typedef struct sdl_t
{
SDL_Window*win;
SDL_Event e;
SDL_Surface*s;
SDL_Renderer*r;
SDL_Texture*t;
SDL_Rect scr_rect;
ram_t*ram;
int halt;
} sdl_t;
void sdl_redraw(SDL_Surface*s,ram_t*ram)
{
uint8_t*p=(uint8_t*)s->pixels;
for(int i=0;i<(s->w*s->h);++i)
p[i]=ram->ram[VRAM+i];
}
#include"sdl.h"
void*sdl_thread(void*d)
{
@ -52,14 +23,10 @@ void*sdl_thread(void*d)
SDL_SetRenderDrawColor(sdl->r,0,0,0,255);
sdl_redraw(sdl->s,sdl->ram);
//{
//uint8_t*p=(uint8_t*)s->pixels;
//for(int i=0;i<(s->w*s->h);++i)
//p[i]=ram->ram[VRAM+i];
//}
// RENDER LOOP -----
while(!*halt)
// Allow other thread to request quit
while(*halt==0)
{
// Redraw screen
@ -68,10 +35,9 @@ void*sdl_thread(void*d)
SDL_RenderCopy(sdl->r,sdl->t,NULL,&sdl->scr_rect);
SDL_RenderPresent(sdl->r);
SDL_WaitEvent(&sdl->e);
// EVENT LOOP -----
if(SDL_PollEvent(&sdl->e))
//if(SDL_PollEvent(&sdl->e))
if(SDL_WaitEvent(&sdl->e))
{
switch(sdl->e.type)
{
@ -80,12 +46,23 @@ void*sdl_thread(void*d)
case SDL_KEYDOWN:
{
SDL_KeyboardEvent k=*((SDL_KeyboardEvent*)&sdl->e);
printf("e.keysym.sym: 0x%04X\n",k.keysym.sym);
//printf("e.keysym.sym: 0x%04X\n",k.keysym.sym);
if(k.keysym.sym>=SDLK_a &&
k.keysym.sym<SDLK_a+26)
printf("key: %c (0x%02X)\n",
k.keysym.sym,k.keysym.sym);
//if(k.keysym.sym>=SDLK_a && k.keysym.sym<SDLK_a+26)
//printf("key: %c (0x%02X)\n",
//k.keysym.sym,k.keysym.sym);
if(k.keysym.sym==SDLK_LEFT)
sdl->joy->buttons.bits.left=1,
puts("left");
if(k.keysym.sym==SDLK_RIGHT)
sdl->joy->buttons.bits.right=1,
puts("right");
if(k.keysym.sym==SDLK_UP)
sdl->joy->buttons.bits.up=1,
puts("up");
if(k.keysym.sym==SDLK_DOWN)
sdl->joy->buttons.bits.down=1,
puts("down");
if(k.keysym.sym==SDLK_ESCAPE)
{
@ -106,76 +83,27 @@ void*sdl_thread(void*d)
quit:
// Free memory
SDL_Quit();
SDL_FreeSurface(sdl->s);
SDL_DestroyRenderer(sdl->r);
SDL_DestroyTexture(sdl->t);
sdl->halt=1;
sdl_del(sdl);
return NULL;
}
int main(int argc,char**argv)
void sdl_del(sdl_t*sdl)
{
cpu_t*cpu=cpu_init();
rom_t*rom=new(rom_t);
ram_t*ram=ram_init();
joy_t*joy=joy_init(JOYPREG,2);
sdl_t*sdl=new(sdl_t);
sdl->ram=ram;
pthread_t sdl_th;
// Parse command line arguments
if(argc>1)
static int already=0;
if(!already)
{
rom_load_file(rom,argv[1]);
rom_map(rom,ram,PRGROM);
SDL_Quit();
if(sdl->s)SDL_FreeSurface(sdl->s);
if(sdl->r)SDL_DestroyRenderer(sdl->r);
if(sdl->t)SDL_DestroyTexture(sdl->t);
sdl->halt=1;
}
// Verify ROM is loaded
if(rom->rom)
cpu->pc=PRGROM;
else
puts("Failed to load ROM");
if(!ram || !rom || !joy)puts("error"),exit(1);
pthread_create(&sdl_th,NULL,sdl_thread,sdl);
// Main loop
for(;;)
{
joy_update_ram(joy,ram);
cpu_exec(cpu,ram);
if(sdl->halt) goto quit;
}
quit:
sdl->halt=1;
pthread_join(sdl_th,NULL);
//pthread_cancel(sdl_th);
free(cpu);
rom_del(rom);
free(rom);
ram_del(ram);
free(ram);
joy_del(joy);
free(joy);
free(sdl);
already=1;
}
// Print formatted error message
void puterr(const char*fmt,...)
void sdl_redraw(SDL_Surface*s,ram_t*ram)
{
va_list list;
va_start(list,fmt);
mvclr(STATUSLINE,0);
mvprintw(STATUSLINE,0,PROG": ");
attron(COLOR_PAIR(2));
printw("Error: ");
attron(COLOR_PAIR(1));
vw_printw(win,fmt,list);
va_end(list);
uint8_t*p=(uint8_t*)s->pixels;
for(int i=0;i<(s->w*s->h);++i)
p[i]=ram->ram[VRAM+i];
}

29
sdl.h Normal file
View File

@ -0,0 +1,29 @@
#include"65.h"
#include"cpu.h"
#include"ram.h"
#include"rom.h"
#include"joy.h"
#include<SDL2/SDL.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
#include<stdio.h>
#include<time.h>
typedef struct sdl_t
{
SDL_Window*win;
SDL_Event e;
SDL_Surface*s;
SDL_Renderer*r;
SDL_Texture*t;
SDL_Rect scr_rect;
ram_t*ram;
joy_t*joy;
int halt;
} sdl_t;
void sdl_del(sdl_t*sdl);
void sdl_redraw(SDL_Surface*s,ram_t*ram);
void*sdl_thread(void*d);

View File

@ -25,5 +25,5 @@ config:
clean:
@echo "RM $(OBJS)"
@$(RM) $(OBJS)
@$(RM) *.s *.o65
@$(RM) *.s *.o65 config
.SUFFIXES: .a65 .nes .c65