Exemplo n.º 1
0
int main(int argc, char **argv) {
    srand(time(NULL));

    if (argc != 2) {
        cout << "Usage is : chipote [ROMFILE]" << endl;
        return EXIT_FAILURE;
    }

    // SFML init
    sf::RenderWindow window;
    window.create(sf::VideoMode(64 * SCALE, 32 * SCALE), "chipote");
    window.setFramerateLimit(60);

    // display init
    sf::Image renderer;
    sf::Texture texture;
    sf::Sprite sprite;

    // help: the image is where the pixels are toggled.
    //       the texture/sprite are just here to show the pixels
    //       easily on the SFML window
    renderer.create(64, 32, sf::Color::Black);
    texture.loadFromImage(renderer);
    sprite.setTexture(texture);
    sprite.setScale(SCALE, SCALE);

    // chip8 init
    Chip8 interpreter;
    interpreter.load(argv[1]);

    // key bindings init
    map<int,int> binding_keys;
    binding_keys[sf::Keyboard::Num1] = uint8_t(0x1);
    binding_keys[sf::Keyboard::Num2] = uint8_t(0x2);
    binding_keys[sf::Keyboard::Num3] = uint8_t(0x3);
    binding_keys[sf::Keyboard::Num4] = uint8_t(0xC);
    binding_keys[sf::Keyboard::A] = uint8_t(0x4);
    binding_keys[sf::Keyboard::Z] = uint8_t(0x5);
    binding_keys[sf::Keyboard::E] = uint8_t(0x6);
    binding_keys[sf::Keyboard::R] = uint8_t(0xD);
    binding_keys[sf::Keyboard::Q] = uint8_t(0x7);
    binding_keys[sf::Keyboard::S] = uint8_t(0x8);
    binding_keys[sf::Keyboard::D] = uint8_t(0x9);
    binding_keys[sf::Keyboard::F] = uint8_t(0xE);
    binding_keys[sf::Keyboard::W] = uint8_t(0xA);
    binding_keys[sf::Keyboard::X] = uint8_t(0x0);
    binding_keys[sf::Keyboard::C] = uint8_t(0xB);
    binding_keys[sf::Keyboard::V] = uint8_t(0xF);

    sf::Event event;
    while (window.isOpen()) {

        // handle events
        while (window.pollEvent(event)) {
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            if (event.type == sf::Event::KeyPressed) {
                interpreter.is_pressed(binding_keys[event.key.code], true);

                if (DEBUG && event.key.code == sf::Keyboard::Space) {
                    interpreter.update();
                }
            }
            if (event.type == sf::Event::KeyReleased) {
                interpreter.is_pressed(binding_keys[event.key.code], false);
            }
        }

        // update
        for (int i = 0; i < 7; i++) {
            if (!DEBUG) {
                interpreter.update(); // step the machine of 1 cycle
            }

            if (interpreter.draw_flag) {
                interpreter.draw_flag = false;

                for (int i = 0; i < 2048; i++) {
                    int py = floor(i / 64);
                    int px = floor(i - (py * 64));

                    if (interpreter.screen[i] == 0) {
                        renderer.setPixel(px, py, sf::Color::Black);
                    }
                    else {
                        renderer.setPixel(px, py, sf::Color::White);
                    }
                }
                texture.loadFromImage(renderer);
            }
        }

        // draw
        window.clear();
        window.draw(sprite);
        window.display();
    }

    return EXIT_SUCCESS;
}
Exemplo n.º 2
0
void Debugger::parse_line(Chip8 &cpu) {
  std::string line;
  std::getline(std::cin, line);

  if (line.compare("step") == 0 || line.compare("s") == 0 ||
      (line.compare("") == 0 && last_command == STEP)) {
    cpu.update(true);
    last_command = STEP;
  } else if (line.compare(0, 5, "step ", 5) == 0) {
    line = line.substr(5, line.size());
    int count = std::stoi(line);
    for (int i = 0; i < count; i++)
    {
        cpu.update(true);
    }
    last_command = STEP;
  } else if (line.compare(0, 2, "s ", 2) == 0) {
    line = line.substr(2, line.size());
    int count = std::stoi(line);
    for (int i = 0; i < count; i++)
    {
        cpu.update(true);
    }
    last_command = STEP;
  } else if (line.compare("exit") == 0 || last_command == EXIT) {
    std::cout << "leaving Emulator at: " << std::setbase(16) << cpu.get_PC()
              << std::endl;
    exit(0);
  } else if (line.compare(0, 6, "print ", 6) == 0) {
    line = line.substr(6, line.size());

    if (line.compare("PC") == 0) {
      std::cout << "PC: " << std::setbase(16) << cpu.get_PC() << std::endl;

    } else if (line.compare("SP") == 0) {
      std::cout << "SP: " << std::setbase(16) << cpu.get_SP() << std::endl;

    } else if (line.compare("I") == 0) {
      std::cout << "I: " << std::setbase(16) << cpu.get_I() << std::endl;

    } else if (line.compare("timer") == 0) {
      std::cout << "TIMER: " << std::setbase(16) << cpu.get_timer()
                << std::endl;

    } else if (line.compare("sound") == 0) {
      std::cout << "SOUND: " << std::setbase(16) << cpu.get_sound()
                << std::endl;

    } else if (line.find_first_of("[") != line.npos) {
      std::string addr =
          line.substr(line.find_first_of("[") + 1, line.find_last_of("]") - 1);
      unsigned int num = std::stoul(addr, 0, 16);
      std::cout << "memory@address[" << std::setbase(16) << num
                << "]: " << (int)cpu.get_mem(num) << std::endl;

    } else if (line.find("V(") != line.npos) {
      std::string addr =
          line.substr(line.find_first_of("(") + 1, line.find_last_of(")") - 1);
      unsigned int num = std::stoul(addr, 0, 16);
      std::cout << "register V(" << std::setbase(16) << num
                << "): " << (int)cpu.get_V_regs(num) << std::endl;

    } else if (line.compare("cpu") == 0) {
      cpu.print_state();
    }
  } else if (line.compare("continue") == 0 || last_command == CONTINUE) {
    while (true) {
      cpu.update();
      last_command = CONTINUE;
    }

  } else {
    std::cout << "Vigilant-Chip8> Command not recognized.\n";
    last_command = INVALID;
  }
}
Exemplo n.º 3
0
int main(int argc, char* argv[])
{
    Chip8 chip;
    if (argc == 2)
    {
        string path(argv[1]);
        chip.loadProgram(path);

        sf::RenderWindow window(sf::VideoMode(1024, 512), "Chip8");
        window.setFramerateLimit(60);
        window.setKeyRepeatEnabled(false);

        sf::Clock clock;

        while (window.isOpen())
        {
            sf::Event event;
            while (window.pollEvent(event))
            {
                if (event.type == sf::Event::Closed)
                    window.close();
                else if (event.type == sf::Event::KeyPressed)
                {
                    switch (event.key.code)
                    {
                        case sf::Keyboard::Num1:
                            chip.pressKey(0);
                            break;
                        case sf::Keyboard::Num2:
                            chip.pressKey(1);
                            break;
                        case sf::Keyboard::Num3:
                            chip.pressKey(2);
                            break;
                        case sf::Keyboard::Num4:
                            chip.pressKey(3);
                            break;
                        case sf::Keyboard::Q:
                            chip.pressKey(4);
                            break;
                        case sf::Keyboard::W:
                            chip.pressKey(5);
                            break;
                        case sf::Keyboard::E:
                            chip.pressKey(6);
                            break;
                        case sf::Keyboard::R:
                            chip.pressKey(7);
                            break;
                        case sf::Keyboard::A:
                            chip.pressKey(8);
                            break;
                        case sf::Keyboard::S:
                            chip.pressKey(9);
                            break;
                        case sf::Keyboard::D:
                            chip.pressKey(10);
                            break;
                        case sf::Keyboard::F:
                            chip.pressKey(11);
                            break;
                        case sf::Keyboard::Z:
                            chip.pressKey(12);
                            break;
                        case sf::Keyboard::X:
                            chip.pressKey(13);
                            break;
                        case sf::Keyboard::C:
                            chip.pressKey(14);
                            break;
                        case sf::Keyboard::V:
                            chip.pressKey(15);
                            break;
                    }
                }
                else if (event.type == sf::Event::KeyReleased)
                {
                    switch (event.key.code)
                    {
                        case sf::Keyboard::Num1:
                            chip.releaseKey(0);
                            break;
                        case sf::Keyboard::Num2:
                            chip.releaseKey(1);
                            break;
                        case sf::Keyboard::Num3:
                            chip.releaseKey(2);
                            break;
                        case sf::Keyboard::Num4:
                            chip.releaseKey(3);
                            break;
                        case sf::Keyboard::Q:
                            chip.releaseKey(4);
                            break;
                        case sf::Keyboard::W:
                            chip.releaseKey(5);
                            break;
                        case sf::Keyboard::E:
                            chip.releaseKey(6);
                            break;
                        case sf::Keyboard::R:
                            chip.releaseKey(7);
                            break;
                        case sf::Keyboard::A:
                            chip.releaseKey(8);
                            break;
                        case sf::Keyboard::S:
                            chip.releaseKey(9);
                            break;
                        case sf::Keyboard::D:
                            chip.releaseKey(10);
                            break;
                        case sf::Keyboard::F:
                            chip.releaseKey(11);
                            break;
                        case sf::Keyboard::Z:
                            chip.releaseKey(12);
                            break;
                        case sf::Keyboard::X:
                            chip.releaseKey(13);
                            break;
                        case sf::Keyboard::C:
                            chip.releaseKey(14);
                            break;
                        case sf::Keyboard::V:
                            chip.releaseKey(15);
                            break;
                    }
                }
            }

            window.clear(sf::Color::Black);

            chip.update(clock.restart().asMilliseconds());

            window.draw(chip);

            window.display();
        }
    }
    return 0;
}