captmicro

Untitled

May 28th, 2011
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.08 KB | None | 0 0
  1. #include <xboxkrnl/xboxkrnl.h>
  2. #include <hal/xbox.h>
  3. #include <hal/video.h>
  4. #include <hal/audio.h>
  5. #include <hal/input.h>
  6. #include <hal/fileio.h>
  7. #include <openxdk/debug.h>
  8. #include <string.h>
  9.  
  10. #undef CHIP8_DEBUG
  11. #define CHIP8_SUPER
  12. #include "CHIP8.h"
  13.  
  14. #define XPAD_AXISTOLERANCE 9000
  15. #define XPAD_AXISINPOS(val) (val >= XPAD_AXISTOLERANCE)
  16. #define XPAD_AXISINNEG(val) (val <= -XPAD_AXISTOLERANCE)
  17. #define XPAD_AXISINLEFT(val) XPAD_AXISINNEG(val)
  18. #define XPAD_AXISINRIGHT(val) XPAD_AXISINPOS(val)
  19. #define XPAD_AXISINUP(val) XPAD_AXISINPOS(val)
  20. #define XPAD_AXISINDOWN(val) XPAD_AXISINNEG(val)
  21.  
  22. #define RGB(r,g,b) ((r) | (g << 8) | (b << 16))
  23. #define RGB_GetR(rgb) ((BYTE)((rgb) & 0xFF))
  24. #define RGB_GetG(rgb) ((BYTE)((rgb) >> 8))
  25. #define RGB_GetB(rgb) ((BYTE)((rgb) >> 16))
  26.  
  27. typedef struct
  28. {
  29.     char sync;
  30.     char _unused0;
  31.     char scale;
  32.     char _unused1;
  33.     char imgpath[256];
  34. } RAWCFG;
  35.  
  36. static int SYNC = 1;                           /* If 0, do not sync emu     */
  37. static int SCALE = 4;                          /* Size of CHIP8 pixels      */
  38.  
  39. BYTE running = 1;
  40. UINT width = 640, height = 480, bpp = 32;
  41. UINT pmultx = 0, pmulty = 0;
  42. BYTE *fb = 0;
  43.  
  44. static DWORD prng_lfsr = 0x9D0A31FC;
  45. DWORD prng_step()
  46. {
  47.     prng_lfsr = (prng_lfsr >> 1) ^ (UINT)(0 - (prng_lfsr & 1) & 0xD0000001);
  48.     return prng_lfsr;
  49. }
  50.  
  51. void PutPixel(UINT x, UINT y, DWORD color)
  52. {
  53.     if (color&0x00FFFFFF == 0x007F0057) return;
  54.     fb[(y*pmulty)+(x*pmultx)] = RGB_GetB(color); //b
  55.     fb[(y*pmulty)+(x*pmultx)+1] = RGB_GetG(color); //g
  56.     fb[(y*pmulty)+(x*pmultx)+2] = RGB_GetR(color); //r
  57. }
  58.  
  59. void PutBox(UINT x, UINT y, UINT w, UINT h, DWORD color)
  60. {
  61.     UINT i = 0, j = 0;
  62.     for (i = 0; i < w; i++)
  63.         for (j = 0; j < h; j++)
  64.             PutPixel(x+i, y+j, color);
  65. }
  66.  
  67. /****************************************************************************/
  68. /* Start emulation                                                          */
  69. /****************************************************************************/
  70. void XBoxStartup()
  71. {
  72.     UINT i = 0;
  73.    
  74.     XVideoSetMode(width, height, bpp, 60);
  75.     XVideoSetSoftenFilter(0);
  76.     XVideoSetFlickerFilter(0);
  77.    
  78.     pmultx = bpp / 8;
  79.     pmulty = width * (bpp / 8);
  80.     fb = XVideoGetFB();
  81.    
  82.     XInput_Init();
  83.     XAudioInit(16, 2, 0, 0);
  84.    
  85.     debugPrint("    Vision-8: Portable CHIP8 emulator\n"
  86.         "    Copyright (C) 1997 Marcel de Kogel\n"
  87.         "    SCHIP support (C) 2005 Frederic Devernay\n"
  88.         "    OpenXDK XBOX port: Capt. Micro\n");
  89.    
  90.     RAWCFG rawcfg;
  91.     memset(&rawcfg, 0, sizeof(RAWCFG));
  92.    
  93.     int cfgfh = 0;
  94.     XCreateFile(&cfgfh , "d:/chip8.cfg", GENERIC_READ,
  95.         FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
  96.     XReadFile(cfgfh, &rawcfg, sizeof(RAWCFG), 0);
  97.     XCloseHandle(cfgfh);
  98.    
  99.     SYNC = (rawcfg.sync-'0'>0)?1:0;
  100.     debugPrint("\n    Sync: %s\n", (SYNC>0)?"TRUE":"FALSE");
  101.     SCALE = rawcfg.scale-'0';
  102.     debugPrint("    Scale: %d\n", SCALE);
  103.    
  104.     debugPrint("    Image: %s\n", rawcfg.imgpath);
  105.    
  106.     int imgfg = 0;
  107.     XCreateFile(&imgfg , rawcfg.imgpath, GENERIC_READ,
  108.         FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
  109.     XReadFile(imgfg, chip8_mem+0x200, 4096-0x200, 0);
  110.     XCloseHandle(imgfg);
  111.    
  112.     debugPrint("\n    Image Preview: ");
  113.     for (i=0;i<12;i++) debugPrint("%02X", chip8_mem+0x200+i);
  114.     debugPrint("\n");
  115.    
  116.     debugPrint("\n    Press START to being\n");
  117.     while ((g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_START)==0) {
  118.         XInput_GetEvents(); prng_step();
  119.     }
  120.    
  121.     //chip8_iperiod = 15;
  122.     chip8_reset();
  123.    
  124.     running = 1;
  125.     while (running == 1)
  126.     {
  127.         for (i=0;i<32;i++)
  128.             PutPixel(50+i, 600, RGB(((prng_lfsr>>i)&1)?255:0,0,0));
  129.         prng_step();
  130.         //chip8_execute();
  131.        
  132.         XInput_GetEvents();
  133.         if (g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_BACK)
  134.             running = 0;
  135.     }
  136.    
  137.     XAudioPause();
  138.     XInput_Quit();
  139.     XReboot();
  140. }
  141.  
  142. /****************************************************************************/
  143. /* Turn sound on                                                            */
  144. /****************************************************************************/
  145. void chip8_sound_on(void)
  146. {
  147.     UINT i = 0;
  148.     for (i=0;i<prng_step()&0xFF;prng_step(),i++);
  149.     BYTE beep[4096];
  150.     for (i=0;i<4096;i++) beep[i] = prng_step() & 0xFF;
  151.     XAudioProvideSamples(beep, 4096, 1);
  152.     XAudioPlay();
  153. }
  154.  
  155. /****************************************************************************/
  156. /* Turn sound off                                                           */
  157. /****************************************************************************/
  158. void chip8_sound_off(void)
  159. {
  160.     XAudioPause();
  161. }
  162.  
  163. /****************************************************************************/
  164. /* Update the display                                                       */
  165. /****************************************************************************/
  166. static void update_display(void)
  167. {
  168.     UINT x = 0, y = 0;
  169.     BYTE *c8disp = chip8_display;
  170.     for (x = 0; x < CHIP8_WIDTH; x++)
  171.         for (y = 0; y < CHIP8_HEIGHT; y++)
  172.             PutBox(x * SCALE, y * SCALE, SCALE, SCALE,
  173.                 RGB(0,c8disp[x+(y*CHIP8_WIDTH)],0));
  174. }
  175.  
  176. /****************************************************************************/
  177. /* Update CHIP8 keyboard status                                             */
  178. /****************************************************************************/
  179. static void update_keys(void)
  180. {
  181.     XInput_GetEvents();
  182.     if (g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_START) {
  183.        
  184.     }
  185.     if (g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_BACK) {
  186.         chip8_running = 0;
  187.         running = 0;
  188.     }
  189.     if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_LEFT_THUMB) {
  190.        
  191.     }
  192.     if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_RIGHT_THUMB) {
  193.        
  194.     }
  195.     if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_UP) {
  196.        
  197.     }
  198.     if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_DOWN) {
  199.        
  200.     }
  201.     if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_LEFT) {
  202.        
  203.     }
  204.     if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_RIGHT) {
  205.        
  206.     }
  207.     if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_A]) {
  208.        
  209.     }
  210.     if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_B]) {
  211.        
  212.     }
  213.     if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_X]) {
  214.        
  215.     }
  216.     if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_Y]) {
  217.        
  218.     }
  219.     if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_BLACK]) {
  220.        
  221.     }
  222.     if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_WHITE]) {
  223.        
  224.     }
  225.     if (XPAD_AXISINUP(g_DefaultPad.sLThumbY)) {
  226.        
  227.     }
  228.     if (XPAD_AXISINDOWN(g_DefaultPad.sLThumbY)) {
  229.        
  230.     }
  231.     if (XPAD_AXISINLEFT(g_DefaultPad.sLThumbX)) {
  232.        
  233.     }
  234.     if (XPAD_AXISINRIGHT(g_DefaultPad.sLThumbX)) {
  235.        
  236.     }
  237.     if (XPAD_AXISINUP(g_DefaultPad.sRThumbY)) {
  238.        
  239.     }
  240.     if (XPAD_AXISINDOWN(g_DefaultPad.sRThumbY)) {
  241.        
  242.     }
  243.     if (XPAD_AXISINLEFT(g_DefaultPad.sRThumbX)) {
  244.        
  245.     }
  246.     if (XPAD_AXISINRIGHT(g_DefaultPad.sRThumbX)) {
  247.        
  248.     }
  249. }
  250.  
  251. /****************************************************************************/
  252. /* Update keyboard and display, sync emulation with hardware timer          */
  253. /****************************************************************************/
  254. void chip8_interrupt(void)
  255. {
  256.     update_display();
  257.     update_keys();
  258.     if (SYNC) XVideoWaitForVBlank();
  259. }
Add Comment
Please, Sign In to add comment