📄 chip8.c
字号:
for (i=0,j=(opcode>>8)&0x0f; i<=j; ++i) write_mem(chip8_regs.i+i,chip8_regs.alg[i]); break; case 0x65: for (i=0,j=(opcode>>8)&0x0f; i<=j; ++i) chip8_regs.alg[i]=read_mem(chip8_regs.i+i); break; }}static void op_sprite (word opcode){ byte *q; byte n,x,x2,y,collision; word p; x=get_reg_value(opcode)&63; y=get_reg_value_2(opcode)&31; p=chip8_regs.i; q=chip8_display+y*64; n=opcode&0x0f; if (n+y>32) n=32-y; for (collision=1;n;--n,q+=64) { for (y=read_mem(p++),x2=x;y;y<<=1,x2=(x2+1)&63) if (y&0x80) collision&=(q[x2]^=0xff); } chip8_regs.alg[15]=collision^1;}static math_fn math_opcodes[16]={ math_mov, math_or, math_and, math_xor, math_add, math_sub, math_shr, math_rsb, math_nop, math_nop, math_nop, math_nop, math_nop, math_nop, math_shl, math_nop};static void op_math (word opcode){ (*(math_opcodes[opcode&0x0f])) (get_reg_offset(opcode),get_reg_value_2(opcode));}static opcode_fn main_opcodes[16]={ op_system, op_jmp, op_call, op_skeq_const, op_skne_const, op_skeq_reg, op_mov_const, op_add_const, op_math, op_skne_reg, op_mvi, op_jmi, op_rand, op_sprite, op_key, op_misc};/****************************************************************************//* Update the display *//****************************************************************************/static void chip8_update_display(void){ int x,y,i; byte w; byte* row; for (y=0;y<=7;++y) /* 32 rows */ { row = lcd_framebuf[y]; for (x=0;x<64;++x) /* 64 columns */ { w = 0; for (i=0;i<=3;i++) { w = w >> 2; if (chip8_display[x+(y*4+i)*64] != 0) { w += 128+64; } } *row++ = w; } } rb->lcd_blit(lcd_framebuf[0], 24, 0, 64, 8, 64);}static void chip8_keyboard(void){ switch (rb->button_get(false)) { case BUTTON_OFF: /* Abort Emulator */ chip8_running = false; break; case BUTTON_UP: chip8_keys[2] = 1; break; case BUTTON_UP | BUTTON_REL: chip8_keys[2] = 0; break; case BUTTON_LEFT: chip8_keys[4] = 1; break; case BUTTON_LEFT | BUTTON_REL: chip8_keys[4] = 0; break; case BUTTON_RIGHT: chip8_keys[6] = 1; break; case BUTTON_RIGHT | BUTTON_REL: chip8_keys[6] = 0; break; case BUTTON_DOWN: chip8_keys[8] = 1; break; case BUTTON_DOWN | BUTTON_REL: chip8_keys[8] = 0; break; case BUTTON_PLAY: chip8_keys[5] = 1; break; case BUTTON_PLAY | BUTTON_REL: chip8_keys[5] = 0; break; case BUTTON_F1: chip8_keys[1] = 1; break; case BUTTON_F1 | BUTTON_REL: chip8_keys[1] = 0; break; case BUTTON_F2: chip8_keys[7] = 1; break; case BUTTON_F2 | BUTTON_REL: chip8_keys[7] = 0; break; case BUTTON_F3: chip8_keys[3] = 1; break; case BUTTON_F3 | BUTTON_REL: chip8_keys[3] = 0; break; case BUTTON_ON: chip8_keys[9] = 1; break; case BUTTON_ON | BUTTON_REL: chip8_keys[9] = 0; break; case SYS_USB_CONNECTED:#ifdef HAVE_LCD_CHARCELLS status_set_param(false);#endif chip8_running = false; break; }}/****************************************************************************//* Execute chip8_iperiod opcodes *//****************************************************************************/static void chip8_execute(void){ byte i; byte key_pressed=0; word opcode; for (i = chip8_iperiod ; i ;--i) { /* Fetch the opcode */ opcode=(read_mem(chip8_regs.pc)<<8)+read_mem(chip8_regs.pc+1); chip8_regs.pc+=2; (*(main_opcodes[opcode>>12]))(opcode&0x0fff); /* Emulate this opcode */ } /* Update timers */ if (chip8_regs.delay) --chip8_regs.delay; if (chip8_regs.sound) if (--chip8_regs.sound == 0) /* Update the machine status */ chip8_sound_off(); chip8_update_display(); chip8_keyboard(); rb->yield(); /* we should regulate the speed by timer query, sleep/yield */ for (i=key_pressed=0;i<16;++i) /* check if a key was first */ if (chip8_keys[i]) key_pressed=i+1; /* pressed */ if (key_pressed && key_pressed!=chip8_key_pressed) chip8_key_pressed=key_pressed; else chip8_key_pressed=0;}/****************************************************************************//* Reset the virtual chip8 machine *//****************************************************************************/static void chip8_reset(void){ static byte chip8_sprites[16*5]= { 0xf9,0x99,0xf2,0x62,0x27, 0xf1,0xf8,0xff,0x1f,0x1f, 0x99,0xf1,0x1f,0x8f,0x1f, 0xf8,0xf9,0xff,0x12,0x44, 0xf9,0xf9,0xff,0x9f,0x1f, 0xf9,0xf9,0x9e,0x9e,0x9e, 0xf8,0x88,0xfe,0x99,0x9e, 0xf8,0xf8,0xff,0x8f,0x88, }; byte i; for (i=0;i<16*5;++i) { write_mem (i<<1,chip8_sprites[i]&0xf0); write_mem ((i<<1)+1,chip8_sprites[i]<<4); } rb->memset (chip8_regs.alg,0,sizeof(chip8_regs.alg)); rb->memset (chip8_keys,0,sizeof(chip8_keys)); chip8_key_pressed=0; rb->memset (chip8_display,0,sizeof(chip8_display)); chip8_regs.delay=chip8_regs.sound=chip8_regs.i=0; chip8_regs.sp=0x1e0; chip8_regs.pc=0x200; chip8_sound_off (); chip8_running=1;}static bool chip8_init(char* file){ int numread; int fd; fd = rb->open(file, O_RDONLY); if (fd==-1) return false; numread = rb->read(fd, chip8_mem+0x200, 4096-0x200); if (numread==-1) return false; rb->close(fd); return true;}bool chip8_run(char* file){ int ok; ok = chip8_init(file); if (!ok) { rb->lcd_clear_display(); rb->lcd_puts(0, 0, "Error"); rb->lcd_update(); rb->sleep(HZ); return false; } rb->lcd_clear_display(); rb->lcd_puts(0, 0, "Chip8 Emulator "); rb->lcd_puts(0, 1, " (c) by "); rb->lcd_puts(0, 2, "Marcel de Kogel"); rb->lcd_puts(0, 3, " Archos: "); rb->lcd_puts(0, 4, " Blueloop "); rb->lcd_puts(0, 5, "---------------"); rb->lcd_puts(0, 6, "File OK..."); rb->lcd_update(); rb->sleep(HZ*1); rb->lcd_clear_display(); rb->lcd_drawrect(23,0,66,64); rb->lcd_update(); /* init sound */ is_playing = rb->mp3_is_playing(); /* would we disturb playback? */ if (!is_playing) /* no? then we can make sound */ { /* prepare */ rb->mp3_play_data(beep, sizeof(beep), callback); } chip8_reset(); while (chip8_running) chip8_execute(); if (!is_playing) { /* stop it if we used audio */ rb->mp3_play_stop(); // stop audio ISR } return true;}/***************** Plugin Entry Point *****************/enum plugin_status plugin_start(struct plugin_api* api, void* parameter){ char* filename; /* this macro should be called as the first thing you do in the plugin. it test that the api version and model the plugin was compiled for matches the machine it is running on */ TEST_PLUGIN_API(api); rb = api; /* copy to global api pointer */ if (parameter == NULL) { rb->lcd_puts(0, 0, "Play .ch8 file!"); rb->lcd_update(); rb->sleep(HZ); return PLUGIN_ERROR; } else { filename = (char*) parameter; } /* now go ahead and have fun! */ return chip8_run(filename) ? PLUGIN_OK : PLUGIN_ERROR;}#endif /* #ifndef SIMULATOR */#endif /* #ifdef HAVE_LCD_BITMAP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -