Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <xboxkrnl/xboxkrnl.h>
- #include <hal/xbox.h>
- #include <hal/video.h>
- #include <hal/audio.h>
- #include <hal/input.h>
- #include <hal/fileio.h>
- #include <openxdk/debug.h>
- #include <string.h>
- #undef CHIP8_DEBUG
- #define CHIP8_SUPER
- #include "CHIP8.h"
- #define XPAD_AXISTOLERANCE 9000
- #define XPAD_AXISINPOS(val) (val >= XPAD_AXISTOLERANCE)
- #define XPAD_AXISINNEG(val) (val <= -XPAD_AXISTOLERANCE)
- #define XPAD_AXISINLEFT(val) XPAD_AXISINNEG(val)
- #define XPAD_AXISINRIGHT(val) XPAD_AXISINPOS(val)
- #define XPAD_AXISINUP(val) XPAD_AXISINPOS(val)
- #define XPAD_AXISINDOWN(val) XPAD_AXISINNEG(val)
- #define RGB(r,g,b) ((r) | (g << 8) | (b << 16))
- #define RGB_GetR(rgb) ((BYTE)((rgb) & 0xFF))
- #define RGB_GetG(rgb) ((BYTE)((rgb) >> 8))
- #define RGB_GetB(rgb) ((BYTE)((rgb) >> 16))
- typedef struct
- {
- char sync;
- char _unused0;
- char scale;
- char _unused1;
- char imgpath[256];
- } RAWCFG;
- static int SYNC = 1; /* If 0, do not sync emu */
- static int SCALE = 4; /* Size of CHIP8 pixels */
- BYTE running = 1;
- UINT width = 640, height = 480, bpp = 32;
- UINT pmultx = 0, pmulty = 0;
- BYTE *fb = 0;
- static DWORD prng_lfsr = 0x9D0A31FC;
- DWORD prng_step()
- {
- prng_lfsr = (prng_lfsr >> 1) ^ (UINT)(0 - (prng_lfsr & 1) & 0xD0000001);
- return prng_lfsr;
- }
- void PutPixel(UINT x, UINT y, DWORD color)
- {
- if (color&0x00FFFFFF == 0x007F0057) return;
- fb[(y*pmulty)+(x*pmultx)] = RGB_GetB(color); //b
- fb[(y*pmulty)+(x*pmultx)+1] = RGB_GetG(color); //g
- fb[(y*pmulty)+(x*pmultx)+2] = RGB_GetR(color); //r
- }
- void PutBox(UINT x, UINT y, UINT w, UINT h, DWORD color)
- {
- UINT i = 0, j = 0;
- for (i = 0; i < w; i++)
- for (j = 0; j < h; j++)
- PutPixel(x+i, y+j, color);
- }
- /****************************************************************************/
- /* Start emulation */
- /****************************************************************************/
- void XBoxStartup()
- {
- UINT i = 0;
- XVideoSetMode(width, height, bpp, 60);
- XVideoSetSoftenFilter(0);
- XVideoSetFlickerFilter(0);
- pmultx = bpp / 8;
- pmulty = width * (bpp / 8);
- fb = XVideoGetFB();
- XInput_Init();
- XAudioInit(16, 2, 0, 0);
- debugPrint(" Vision-8: Portable CHIP8 emulator\n"
- " Copyright (C) 1997 Marcel de Kogel\n"
- " SCHIP support (C) 2005 Frederic Devernay\n"
- " OpenXDK XBOX port: Capt. Micro\n");
- RAWCFG rawcfg;
- memset(&rawcfg, 0, sizeof(RAWCFG));
- int cfgfh = 0;
- XCreateFile(&cfgfh , "d:/chip8.cfg", GENERIC_READ,
- FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
- XReadFile(cfgfh, &rawcfg, sizeof(RAWCFG), 0);
- XCloseHandle(cfgfh);
- SYNC = (rawcfg.sync-'0'>0)?1:0;
- debugPrint("\n Sync: %s\n", (SYNC>0)?"TRUE":"FALSE");
- SCALE = rawcfg.scale-'0';
- debugPrint(" Scale: %d\n", SCALE);
- debugPrint(" Image: %s\n", rawcfg.imgpath);
- int imgfg = 0;
- XCreateFile(&imgfg , rawcfg.imgpath, GENERIC_READ,
- FILE_SHARE_READ, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL);
- XReadFile(imgfg, chip8_mem+0x200, 4096-0x200, 0);
- XCloseHandle(imgfg);
- debugPrint("\n Image Preview: ");
- for (i=0;i<12;i++) debugPrint("%02X", chip8_mem+0x200+i);
- debugPrint("\n");
- debugPrint("\n Press START to being\n");
- while ((g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_START)==0) {
- XInput_GetEvents(); prng_step();
- }
- //chip8_iperiod = 15;
- chip8_reset();
- running = 1;
- while (running == 1)
- {
- for (i=0;i<32;i++)
- PutPixel(50+i, 600, RGB(((prng_lfsr>>i)&1)?255:0,0,0));
- prng_step();
- //chip8_execute();
- XInput_GetEvents();
- if (g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_BACK)
- running = 0;
- }
- XAudioPause();
- XInput_Quit();
- XReboot();
- }
- /****************************************************************************/
- /* Turn sound on */
- /****************************************************************************/
- void chip8_sound_on(void)
- {
- UINT i = 0;
- for (i=0;i<prng_step()&0xFF;prng_step(),i++);
- BYTE beep[4096];
- for (i=0;i<4096;i++) beep[i] = prng_step() & 0xFF;
- XAudioProvideSamples(beep, 4096, 1);
- XAudioPlay();
- }
- /****************************************************************************/
- /* Turn sound off */
- /****************************************************************************/
- void chip8_sound_off(void)
- {
- XAudioPause();
- }
- /****************************************************************************/
- /* Update the display */
- /****************************************************************************/
- static void update_display(void)
- {
- UINT x = 0, y = 0;
- BYTE *c8disp = chip8_display;
- for (x = 0; x < CHIP8_WIDTH; x++)
- for (y = 0; y < CHIP8_HEIGHT; y++)
- PutBox(x * SCALE, y * SCALE, SCALE, SCALE,
- RGB(0,c8disp[x+(y*CHIP8_WIDTH)],0));
- }
- /****************************************************************************/
- /* Update CHIP8 keyboard status */
- /****************************************************************************/
- static void update_keys(void)
- {
- XInput_GetEvents();
- if (g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_START) {
- }
- if (g_DefaultPad.PressedButtons.usDigitalButtons & XPAD_BACK) {
- chip8_running = 0;
- running = 0;
- }
- if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_LEFT_THUMB) {
- }
- if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_RIGHT_THUMB) {
- }
- if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_UP) {
- }
- if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_DOWN) {
- }
- if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_LEFT) {
- }
- if (g_DefaultPad.CurrentButtons.usDigitalButtons & XPAD_DPAD_RIGHT) {
- }
- if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_A]) {
- }
- if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_B]) {
- }
- if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_X]) {
- }
- if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_Y]) {
- }
- if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_BLACK]) {
- }
- if (g_DefaultPad.PressedButtons.ucAnalogButtons[XPAD_WHITE]) {
- }
- if (XPAD_AXISINUP(g_DefaultPad.sLThumbY)) {
- }
- if (XPAD_AXISINDOWN(g_DefaultPad.sLThumbY)) {
- }
- if (XPAD_AXISINLEFT(g_DefaultPad.sLThumbX)) {
- }
- if (XPAD_AXISINRIGHT(g_DefaultPad.sLThumbX)) {
- }
- if (XPAD_AXISINUP(g_DefaultPad.sRThumbY)) {
- }
- if (XPAD_AXISINDOWN(g_DefaultPad.sRThumbY)) {
- }
- if (XPAD_AXISINLEFT(g_DefaultPad.sRThumbX)) {
- }
- if (XPAD_AXISINRIGHT(g_DefaultPad.sRThumbX)) {
- }
- }
- /****************************************************************************/
- /* Update keyboard and display, sync emulation with hardware timer */
- /****************************************************************************/
- void chip8_interrupt(void)
- {
- update_display();
- update_keys();
- if (SYNC) XVideoWaitForVBlank();
- }
Add Comment
Please, Sign In to add comment