arm 2440 linux 应用程序 nes 红白机模拟器 第2篇 InfoNES
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了arm 2440 linux 应用程序 nes 红白机模拟器 第2篇 InfoNES,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含18452字,纯文字阅读大概需要27分钟。
内容图文
InfoNES 支持 map ,声音,代码比较少,方便 移值。
在上个 LiteNES 的基础上,其实不到半小时就移值好了这个,但问题是,一直是黑屏。InfoNES_LoadFrame () WorkFrame 中一直是 0 。
解决的过程很漫长,最终看到 说是 ADS 中 有符号 无符号的问题,但是 这里用的是 makefile 不是 ADS ,试着改了 makefile 加上 CCFLAGS = -O2 -fsigned-char 。
终于有输出了,性能还算不错。
InfoNES 源码 http://www.zophar.net/pocket-pc/nes/infonesft.html
主要修改的是 InfoNES_System_Linux.cpp 和 Makefile
同样的,现在仅实现显示未加输入,声音,全屏等。后期在加。
InfoNES_System_Linux.cpp
1 /* =================================================================== */ 2 /* */ 3 /* InfoNES_System_Linux.cpp : Linux specific File */ 4 /* */ 5 /* 2001/05/18 InfoNES Project ( Sound is based on DarcNES ) */ 6 /* */ 7 /* =================================================================== */ 8 9 /* ------------------------------------------------------------------- */ 10 /* Include files */ 11 /* ------------------------------------------------------------------- */ 12 13 #include <stdio.h> 14 #include <stdlib.h> 15 #include <string.h> 16 #include <pthread.h> 17 18 #include <sys/types.h> 19 #include <sys/stat.h> 20 #include <fcntl.h> 21 #include <sys/ioctl.h> 22 #include <unistd.h> 23 #include <sys/soundcard.h> 24 25 #include "../InfoNES.h" 26 #include "../InfoNES_System.h" 27 #include "../InfoNES_pAPU.h" 28 29//bool define 30#define TRUE 1 31#define FALSE 0 32 33/* lcd 操作相关 头文件 */ 34 #include <sys/types.h> 35 #include <sys/stat.h> 36 #include <fcntl.h> 37 #include <linux/fb.h> 38 #include <sys/ioctl.h> 39 #include <unistd.h> 40 #include <string.h> 41 #include <sys/mman.h> 42 43staticint fb_fd; 44static unsigned char *fb_mem; 45staticint px_width; 46staticint line_width; 47staticint screen_width; 48staticstruct fb_var_screeninfo var; 49 50staticint lcd_fb_display_px(WORD color, int x, int y) 51{ 52 unsigned char *pen8; 53 unsigned short *pen16; 54 55 pen8 = (unsigned char *)(fb_mem + y*line_width + x*px_width); 56 pen16 = (unsigned short *)pen8; 57 *pen16 = color; 58 59return0; 60} 61 62staticint lcd_fb_init() 63{ 64//如果使用 mmap 打开方式 必须是 读定方式 65 fb_fd = open("/dev/fb0", O_RDWR); 66if(-1 == fb_fd) 67 { 68 printf("cat‘t open /dev/fb0 \n"); 69return -1; 70 } 71//获取屏幕参数 72if(-1 == ioctl(fb_fd, FBIOGET_VSCREENINFO, &var)) 73 { 74 close(fb_fd); 75 printf("cat‘t ioctl /dev/fb0 \n"); 76return -1; 77 } 78 79//计算参数 80 px_width = var.bits_per_pixel / 8; 81 line_width = var.xres * px_width; 82 screen_width = var.yres * line_width; 83 84 fb_mem = (unsigned char *)mmap(NULL, screen_width, PROT_READ | PROT_WRITE, MAP_SHARED, fb_fd, 0); 85if(fb_mem == (void *)-1) 86 { 87 close(fb_fd); 88 printf("cat‘t mmap /dev/fb0 \n"); 89return -1; 90 } 91//清屏 92 memset(fb_mem, 0 , screen_width); 93return0; 94} 95 96/*-------------------------------------------------------------------*/ 97/* ROM image file information */ 98/*-------------------------------------------------------------------*/ 99100char szRomName[256]; 101char szSaveName[256]; 102int nSRAM_SaveFlag; 103104/*-------------------------------------------------------------------*/105/* Constants ( Linux specific ) */106/*-------------------------------------------------------------------*/107108#define VBOX_SIZE 7 109#define SOUND_DEVICE "/dev/dsp" 110#define VERSION "InfoNES v0.91J" 111112/*-------------------------------------------------------------------*/113/* Global Variables ( Linux specific ) */114/*-------------------------------------------------------------------*/115116/* Emulation thread */117pthread_t emulation_tid; 118int bThread; 119120/* Pad state */121DWORD dwKeyPad1; 122DWORD dwKeyPad2; 123DWORD dwKeySystem; 124125/* For Sound Emulation */126 BYTE final_wave[2048]; 127int waveptr; 128int wavflag; 129int sound_fd; 130131/*-------------------------------------------------------------------*/132/* Function prototypes ( Linux specific ) */133/*-------------------------------------------------------------------*/134135void *emulation_thread( void *args ); 136137138void start_application( char *filename ); 139140141int LoadSRAM(); 142143144int SaveSRAM(); 145146147/* Palette data */148 WORD NesPalette[64] = 149{ 1500x39ce, 0x1071, 0x0015, 0x2013, 0x440e, 0x5402, 0x5000, 0x3c20, 1510x20a0, 0x0100, 0x0140, 0x00e2, 0x0ceb, 0x0000, 0x0000, 0x0000, 1520x5ef7, 0x01dd, 0x10fd, 0x401e, 0x5c17, 0x700b, 0x6ca0, 0x6521, 1530x45c0, 0x0240, 0x02a0, 0x0247, 0x0211, 0x0000, 0x0000, 0x0000, 1540x7fff, 0x1eff, 0x2e5f, 0x223f, 0x79ff, 0x7dd6, 0x7dcc, 0x7e67, 1550x7ae7, 0x4342, 0x2769, 0x2ff3, 0x03bb, 0x0000, 0x0000, 0x0000, 1560x7fff, 0x579f, 0x635f, 0x6b3f, 0x7f1f, 0x7f1b, 0x7ef6, 0x7f75, 1570x7f94, 0x73f4, 0x57d7, 0x5bf9, 0x4ffe, 0x0000, 0x0000, 0x0000158}; 159160/*===================================================================*/161/**/162/* main() : Application main */163/**/164/*===================================================================*/165166/* Application main */167int main( int argc, char **argv ) 168{ 169int i; 170171/*-------------------------------------------------------------------*/172/* Pad Control */173/*-------------------------------------------------------------------*/174175/* Initialize a pad state */176 dwKeyPad1 = 0; 177 dwKeyPad2 = 0; 178 dwKeySystem = 0; 179180/*-------------------------------------------------------------------*/181/* Load Cassette & Create Thread */182/*-------------------------------------------------------------------*/183184/* Initialize thread state */185 bThread = FALSE; 186187/* If a rom name specified, start it */188if ( argc == 2 ) 189 { 190 start_application( argv[1] ); 191 } 192193 lcd_fb_init(); 194195//主循环不要让程序退出196while(1) 197 { 198199 } 200return(0); 201} 202203204/*===================================================================*/205/**/206/* emulation_thread() : Thread Hooking Routine */207/**/208/*===================================================================*/209210void *emulation_thread( void *args ) 211{ 212 InfoNES_Main(); 213} 214215216/*===================================================================*/217/**/218/* start_application() : Start NES Hardware */219/**/220/*===================================================================*/221void start_application( char *filename ) 222{ 223/* Set a ROM image name */224 strcpy( szRomName, filename ); 225226/* Load cassette */227if ( InfoNES_Load( szRomName ) == 0 ) 228 { 229/* Load SRAM */230 LoadSRAM(); 231232/* Create Emulation Thread */233 bThread = TRUE; 234 pthread_create( &emulation_tid, NULL, emulation_thread, NULL ); 235 } 236} 237238239/*===================================================================*/240/**/241/* LoadSRAM() : Load a SRAM */242/**/243/*===================================================================*/244int LoadSRAM() 245{ 246/*247 * Load a SRAM 248 * 249 * Return values 250 * 0 : Normally 251 * -1 : SRAM data couldn‘t be read 252*/253254 FILE *fp; 255 unsigned char pSrcBuf[SRAM_SIZE]; 256 unsigned char chData; 257 unsigned char chTag; 258int nRunLen; 259int nDecoded; 260int nDecLen; 261int nIdx; 262263/* It doesn‘t need to save it */264 nSRAM_SaveFlag = 0; 265266/* It is finished if the ROM doesn‘t have SRAM */267if ( !ROM_SRAM ) 268return(0); 269270/* There is necessity to save it */271 nSRAM_SaveFlag = 1; 272273/* The preparation of the SRAM file name */274 strcpy( szSaveName, szRomName ); 275 strcpy( strrchr( szSaveName, ‘.‘ ) + 1, "srm" ); 276277/*-------------------------------------------------------------------*/278/* Read a SRAM data */279/*-------------------------------------------------------------------*/280281/* Open SRAM file */282 fp = fopen( szSaveName, "rb" ); 283if ( fp == NULL ) 284return(-1); 285286/* Read SRAM data */287 fread( pSrcBuf, SRAM_SIZE, 1, fp ); 288289/* Close SRAM file */290 fclose( fp ); 291292/*-------------------------------------------------------------------*/293/* Extract a SRAM data */294/*-------------------------------------------------------------------*/295296 nDecoded = 0; 297 nDecLen = 0; 298299 chTag = pSrcBuf[nDecoded++]; 300301while ( nDecLen < 8192 ) 302 { 303 chData = pSrcBuf[nDecoded++]; 304305if ( chData == chTag ) 306 { 307 chData = pSrcBuf[nDecoded++]; 308 nRunLen = pSrcBuf[nDecoded++]; 309for ( nIdx = 0; nIdx < nRunLen + 1; ++nIdx ) 310 { 311 SRAM[nDecLen++] = chData; 312 } 313 }else { 314 SRAM[nDecLen++] = chData; 315 } 316 } 317318/* Successful */319return(0); 320} 321322323/*===================================================================*/324/**/325/* SaveSRAM() : Save a SRAM */326/**/327/*===================================================================*/328int SaveSRAM() 329{ 330/*331 * Save a SRAM 332 * 333 * Return values 334 * 0 : Normally 335 * -1 : SRAM data couldn‘t be written 336*/337338 FILE *fp; 339int nUsedTable[256]; 340 unsigned char chData; 341 unsigned char chPrevData; 342 unsigned char chTag; 343int nIdx; 344int nEncoded; 345int nEncLen; 346int nRunLen; 347 unsigned char pDstBuf[SRAM_SIZE]; 348349if ( !nSRAM_SaveFlag ) 350return(0); /* It doesn‘t need to save it */351352/*-------------------------------------------------------------------*/353/* Compress a SRAM data */354/*-------------------------------------------------------------------*/355356 memset( nUsedTable, 0, sizeof nUsedTable ); 357358for ( nIdx = 0; nIdx < SRAM_SIZE; ++nIdx ) 359 { 360 ++nUsedTable[SRAM[nIdx++]]; 361 } 362for ( nIdx = 1, chTag = 0; nIdx < 256; ++nIdx ) 363 { 364if ( nUsedTable[nIdx] < nUsedTable[chTag] ) 365 chTag = nIdx; 366 } 367368 nEncoded = 0; 369 nEncLen = 0; 370 nRunLen = 1; 371372 pDstBuf[nEncLen++] = chTag; 373374 chPrevData = SRAM[nEncoded++]; 375376while ( nEncoded < SRAM_SIZE && nEncLen < SRAM_SIZE - 133 ) 377 { 378 chData = SRAM[nEncoded++]; 379380if ( chPrevData == chData && nRunLen < 256 ) 381 ++nRunLen; 382else{ 383if ( nRunLen >= 4 || chPrevData == chTag ) 384 { 385 pDstBuf[nEncLen++] = chTag; 386 pDstBuf[nEncLen++] = chPrevData; 387 pDstBuf[nEncLen++] = nRunLen - 1; 388 }else { 389for ( nIdx = 0; nIdx < nRunLen; ++nIdx ) 390 pDstBuf[nEncLen++] = chPrevData; 391 } 392393 chPrevData = chData; 394 nRunLen = 1; 395 } 396 } 397if ( nRunLen >= 4 || chPrevData == chTag ) 398 { 399 pDstBuf[nEncLen++] = chTag; 400 pDstBuf[nEncLen++] = chPrevData; 401 pDstBuf[nEncLen++] = nRunLen - 1; 402 }else { 403for ( nIdx = 0; nIdx < nRunLen; ++nIdx ) 404 pDstBuf[nEncLen++] = chPrevData; 405 } 406407/*-------------------------------------------------------------------*/408/* Write a SRAM data */409/*-------------------------------------------------------------------*/410411/* Open SRAM file */412 fp = fopen( szSaveName, "wb" ); 413if ( fp == NULL ) 414return(-1); 415416/* Write SRAM data */417 fwrite( pDstBuf, nEncLen, 1, fp ); 418419/* Close SRAM file */420 fclose( fp ); 421422/* Successful */423return(0); 424} 425426427/*===================================================================*/428/**/429/* InfoNES_Menu() : Menu screen */430/**/431/*===================================================================*/432int InfoNES_Menu() 433{ 434/*435 * Menu screen 436 * 437 * Return values 438 * 0 : Normally 439 * -1 : Exit InfoNES 440*/441442/* If terminated */443if ( bThread == FALSE ) 444 { 445return(-1); 446 } 447448/* Nothing to do here */449return(0); 450} 451452453/*===================================================================*/454/**/455/* InfoNES_ReadRom() : Read ROM image file */456/**/457/*===================================================================*/458int InfoNES_ReadRom( constchar *pszFileName ) 459{ 460/*461 * Read ROM image file 462 * 463 * Parameters 464 * const char *pszFileName (Read) 465 * 466 * Return values 467 * 0 : Normally 468 * -1 : Error 469*/470471 FILE *fp; 472473/* Open ROM file */474 fp = fopen( pszFileName, "rb" ); 475if ( fp == NULL ) 476return(-1); 477478/* Read ROM Header */479 fread( &NesHeader, sizeof NesHeader, 1, fp ); 480if ( memcmp( NesHeader.byID, "NES\x1a", 4 ) != 0 ) 481 { 482/* not .nes file */483 fclose( fp ); 484return(-1); 485 } 486487/* Clear SRAM */488 memset( SRAM, 0, SRAM_SIZE ); 489490/* If trainer presents Read Triner at 0x7000-0x71ff */491if ( NesHeader.byInfo1 & 4 ) 492 { 493 fread( &SRAM[0x1000], 512, 1, fp ); 494 } 495496/* Allocate Memory for ROM Image */497 ROM = (BYTE *) malloc( NesHeader.byRomSize * 0x4000 ); 498499/* Read ROM Image */500 fread( ROM, 0x4000, NesHeader.byRomSize, fp ); 501502if ( NesHeader.byVRomSize > 0 ) 503 { 504/* Allocate Memory for VROM Image */505 VROM = (BYTE *) malloc( NesHeader.byVRomSize * 0x2000 ); 506507/* Read VROM Image */508 fread( VROM, 0x2000, NesHeader.byVRomSize, fp ); 509 } 510511/* File close */512 fclose( fp ); 513514/* Successful */515return(0); 516} 517518519/*===================================================================*/520/**/521/* InfoNES_ReleaseRom() : Release a memory for ROM */522/**/523/*===================================================================*/524void InfoNES_ReleaseRom() 525{ 526/*527 * Release a memory for ROM 528 * 529*/530531if ( ROM ) 532 { 533free( ROM ); 534 ROM = NULL; 535 } 536537if ( VROM ) 538 { 539free( VROM ); 540 VROM = NULL; 541 } 542} 543544545/*===================================================================*/546/**/547/* InfoNES_MemoryCopy() : memcpy */548/**/549/*===================================================================*/550void *InfoNES_MemoryCopy( void *dest, constvoid *src, int count ) 551{ 552/*553 * memcpy 554 * 555 * Parameters 556 * void *dest (Write) 557 * Points to the starting address of the copied block‘s destination 558 * 559 * const void *src (Read) 560 * Points to the starting address of the block of memory to copy 561 * 562 * int count (Read) 563 * Specifies the size, in bytes, of the block of memory to copy 564 * 565 * Return values 566 * Pointer of destination 567*/568569 memcpy( dest, src, count ); 570return(dest); 571} 572573574/*===================================================================*/575/**/576/* InfoNES_MemorySet() : memset */577/**/578/*===================================================================*/579void *InfoNES_MemorySet( void *dest, int c, int count ) 580{ 581/*582 * memset 583 * 584 * Parameters 585 * void *dest (Write) 586 * Points to the starting address of the block of memory to fill 587 * 588 * int c (Read) 589 * Specifies the byte value with which to fill the memory block 590 * 591 * int count (Read) 592 * Specifies the size, in bytes, of the block of memory to fill 593 * 594 * Return values 595 * Pointer of destination 596*/597598 memset( dest, c, count ); 599return(dest); 600} 601602603/*===================================================================*/604/**/605/* InfoNES_LoadFrame() : */606/* Transfer the contents of work frame on the screen */607/**/608/*===================================================================*/609void InfoNES_LoadFrame() 610{ 611int x,y; 612 WORD wColor; 613for (y = 0; y < NES_DISP_HEIGHT; y++ ) 614 { 615for (x = 0; x < NES_DISP_WIDTH; x++ ) 616 { 617 wColor = WorkFrame[y * NES_DISP_WIDTH + x ]; 618 lcd_fb_display_px(wColor, x, y); 619 } 620 } 621} 622623624/*===================================================================*/625/**/626/* InfoNES_PadState() : Get a joypad state */627/**/628/*===================================================================*/629void InfoNES_PadState( DWORD *pdwPad1, DWORD *pdwPad2, DWORD *pdwSystem ) 630{ 631/*632 * Get a joypad state 633 * 634 * Parameters 635 * DWORD *pdwPad1 (Write) 636 * Joypad 1 State 637 * 638 * DWORD *pdwPad2 (Write) 639 * Joypad 2 State 640 * 641 * DWORD *pdwSystem (Write) 642 * Input for InfoNES 643 * 644*/645646/* Transfer joypad state */647 *pdwPad1 = dwKeyPad1; 648 *pdwPad2 = dwKeyPad2; 649 *pdwSystem = dwKeySystem; 650} 651652653/*===================================================================*/654/**/655/* InfoNES_SoundInit() : Sound Emulation Initialize */656/**/657/*===================================================================*/658void InfoNES_SoundInit( void ) 659{ 660 sound_fd = 0; 661} 662663664/*===================================================================*/665/**/666/* InfoNES_SoundOpen() : Sound Open */667/**/668/*===================================================================*/669int InfoNES_SoundOpen( int samples_per_sync, int sample_rate ) 670{ 671return1; 672} 673674675/*===================================================================*/676/**/677/* InfoNES_SoundClose() : Sound Close */678/**/679/*===================================================================*/680void InfoNES_SoundClose( void ) 681{ 682if ( sound_fd ) 683 { 684 close( sound_fd ); 685 } 686} 687688689/*===================================================================*/690/**/691/* InfoNES_SoundOutput() : Sound Output 5 Waves */692/**/693/*===================================================================*/694void InfoNES_SoundOutput( int samples, BYTE *wave1, BYTE *wave2, BYTE *wave3, BYTE *wave4, BYTE *wave5 ) 695{ 696697} 698699700/*===================================================================*/701/**/702/* InfoNES_Wait() : Wait Emulation if required */703/**/704/*===================================================================*/705void InfoNES_Wait() 706{ 707} 708709710/*===================================================================*/711/**/712/* InfoNES_MessageBox() : Print System Message */713/**/714/*===================================================================*/715void InfoNES_MessageBox( char *pszMsg, ... ) 716{ 717 printf( "MessageBox: %s \n", pszMsg ); 718} 719720721/*722 * End of InfoNES_System_Linux.cpp 723*/
Makefile
1 CC = arm-linux-gcc 2 TARBALL = InfoNES08J 3 4# InfoNES 5 .CFILES = ./../K6502.cpp 6 ./../InfoNES.cpp 7 ./../InfoNES_Mapper.cpp 8 ./../InfoNES_pAPU.cpp 9 ./InfoNES_System_Linux.cpp 1011 .OFILES = $(.CFILES:.cpp=.o) 1213 CCFLAGS = -O2 -fsigned-char14 LDFILGS = -lstdc++ # gcc3.x.x 1516all: InfoNES 1718InfoNES: $(.OFILES) 19 $(CC) $(INCLUDES) -o $@ $(.OFILES) $(LDFILGS) -lm -lz -lpthread 2021.cpp.o: 22 $(CC) $(INCLUDES) -c $(CCFLAGS) $*.cpp -o $@ 2324clean: 25 rm -f $(.OFILES) ../*~ ../*/*~ core 2627cleanall: 28 rm -f $(.OFILES) ../*~ ../*/*~ core InfoNES 2930release: clean all 3132tar: 33 ( cd ..; 34 tar cvf $(TARBALL).tar ./*; 35 gzip $(TARBALL).tar 36 ) 3738install: 39 install ./InfoNES /usr/local/bin
原文:http://www.cnblogs.com/ningci/p/5620257.html
内容总结
以上是互联网集市为您收集整理的arm 2440 linux 应用程序 nes 红白机模拟器 第2篇 InfoNES全部内容,希望文章能够帮你解决arm 2440 linux 应用程序 nes 红白机模拟器 第2篇 InfoNES所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。