Files
z80/main.cpp

277 lines
9.2 KiB
C++

#include <stdint.h>
#include <stdio.h>
#include "z80.h"
#include "z80dis.h"
#include "z80debug.h"
#include "zx_ula.h"
#include "zx_screen.h"
#include "zx_tape.h"
#include <SDL2/SDL.h>
#include <string.h>
#include "ui.h"
#include "ui_menu.h"
#include "z80analyze.h"
#include "ui_window.h"
#include "zx_mem.h"
//#include "zx_48mem.h"
//#include "zx_128mem.h"
#include "z80viewer.h"
//#include "zx_128bankviewer.h"
//#include "zx_128pageviewer.h"
//uint8_t memory[65536];
uint32_t time = 0;
uint32_t t_states = 0;
namespace actions
{
void exitMenu()
{
const uint8_t dt = z80::step();
z80debug::cont();
zxscreen::refresh(dt);
}
int fastload(int value)
{
zx_tape::toggleOption(ZXTAPE_OPTION_FAST_LOAD);
return zx_tape::getOption(ZXTAPE_OPTION_FAST_LOAD);
}
int stopatend(int value)
{
zx_tape::toggleOption(ZXTAPE_OPTION_STOP_AT_END);
return zx_tape::getOption(ZXTAPE_OPTION_STOP_AT_END);
}
int decZoom(int value)
{
zxscreen::decZoom();
return 0;
}
int incZoom(int value)
{
zxscreen::incZoom();
return 0;
}
int fullscreen(int value)
{
zxscreen::toggleFullscreen();
return zxscreen::getFullscreen();
}
int fullrefresh(int value)
{
zxscreen::toggleFullRefresh();
return zxscreen::getFullRefresh();
}
int showAnalyzer(int value)
{
z80analyze::show();
return 0;
}
}
int main(int argc, char *argv[])
{
//const uint32_t clock = 3500000;
const uint32_t clock = 3546900;
const uint32_t update_freq = clock / 10;
//new zx_48mem();
//new zx_128mem();
mem::init(ZX_128K);
z80dis::loadSymbols();
z80::reset();
z80::connect_port(0xfe, 0x0001, zx_ula::port_in, zx_ula::port_out);
SDL_Init(SDL_INIT_EVERYTHING);
z80debug::init();
//zxscreen::init(SCREEN_MODE_48K);
zxscreen::init(SCREEN_MODE_128K);
//z80viewer *v = new zx_128bankviewer();
//v->show();
//z80viewer::registerViewer("128BANK", v);
//v = new zx_128pageviewer();
//v->show();
//z80viewer::registerViewer("128PAGE", v);
ui::menu::init();
ui::menu::setexitcallback(actions::exitMenu);
int menu = ui::menu::addsubmenu("FILE");
ui::menu::addoption(menu, "LOAD TAPE", nullptr);
ui::menu::addoption(menu, "SAVE TAPE", nullptr);
ui::menu::addseparator(menu);
ui::menu::addoption(menu, "LOAD STATE", nullptr);
ui::menu::addoption(menu, "SAVE STATE", nullptr);
menu = ui::menu::addsubmenu("TAPE");
ui::menu::addbooloption(menu, "BERSERK MODE", zx_tape::getOption(ZXTAPE_OPTION_FAST_LOAD), actions::fastload);
ui::menu::addbooloption(menu, "STOP AT END", zx_tape::getOption(ZXTAPE_OPTION_STOP_AT_END), actions::stopatend);
menu = ui::menu::addsubmenu("SCREEN");
ui::menu::addoption(menu, "DEC ZOOM", actions::decZoom);
ui::menu::addoption(menu, "INC ZOOM", actions::incZoom);
ui::menu::addbooloption(menu, "FULLSCREEN", zxscreen::getFullscreen(), actions::fullscreen);
ui::menu::addseparator(menu);
ui::menu::addbooloption(menu, "FULL REFRESH", zxscreen::getFullRefresh(), actions::fullrefresh);
menu = ui::menu::addsubmenu("EMULATION");
ui::menu::addbooloption(menu, "STOP ON INVALID OP", z80::getOption(Z80_OPTION_STOP_ON_INVALID), actions::decZoom);
ui::menu::addoption(menu, "SHOW ANALYZER", actions::showAnalyzer);
zx_ula::sound_init();
zx_tape::load("ROBOCOP1.TAP");
if (argc==3) { z80debug::loadngo(argv[1], argv[2]); }
z80debug::stop();
bool should_exit = false;
SDL_Event e;
time = SDL_GetTicks();
t_states = 0;
while (!should_exit)
{
while (SDL_PollEvent(&e))
{
bool result = true;
if (e.type == SDL_QUIT) { should_exit=true; break; }
if (e.type == SDL_MOUSEBUTTONDOWN) result = ui::window::sendEvent(e.button.windowID, &e);
if (e.type == SDL_MOUSEBUTTONUP) result = ui::window::sendEvent(e.button.windowID, &e);
if (e.type == SDL_MOUSEMOTION) result = ui::window::sendEvent(e.motion.windowID, &e);
if (e.type == SDL_WINDOWEVENT) result = ui::window::sendEvent(e.window.windowID, &e);
if (e.type == SDL_MOUSEWHEEL) result = ui::window::sendEvent(e.wheel.windowID, &e);
if (e.type == SDL_TEXTINPUT) result = ui::window::sendEvent(e.text.windowID, &e);
if (e.type == SDL_KEYDOWN) {
if (e.key.keysym.scancode==SDL_SCANCODE_F5) {
if (z80debug::debugging()) {
z80debug::history::gototop();
const uint8_t dt = z80::step();
z80debug::cont();
zxscreen::refresh(dt);
}
} else if (e.key.keysym.scancode==SDL_SCANCODE_F8) {
if (!z80debug::debugging()) {
z80debug::stop();
zxscreen::redraw();
} else {
z80debug::show();
}
z80viewer::refreshAll();
} else if (e.key.keysym.scancode==SDL_SCANCODE_F10) {
if (z80debug::debugging()) {
z80debug::show();
z80debug::history::gototop();
const uint8_t dt = z80::step();
z80debug::refresh();
zxscreen::fullrefresh();
zxscreen::redraw();
z80analyze::refresh();
z80viewer::refreshAll();
}
} else if (e.key.keysym.scancode==SDL_SCANCODE_F11) {
if (z80debug::debugging()) {
z80debug::show();
z80debug::history::gototop();
const uint8_t dt = z80debug::next();
z80debug::refresh();
zxscreen::refresh(dt);
zxscreen::redraw();
z80analyze::refresh();
z80viewer::refreshAll();
}
} else if (e.key.keysym.scancode==SDL_SCANCODE_F12) {
if (z80debug::debugging()) {
z80debug::show();
z80debug::history::gototop();
const uint8_t dt = z80debug::stepout();
z80debug::refresh();
zxscreen::refresh(dt);
zxscreen::redraw();
z80analyze::refresh();
z80viewer::refreshAll();
}
}
result = ui::window::sendEvent(e.key.windowID, &e);
}
if (e.type == SDL_MOUSEBUTTONUP && e.button.button==1) ui::setClicked(true);
if (!result)
should_exit = true; break;
}
if (!z80debug::debugging() && !z80debug::paused()) {
//if (z80::getPC()==0x05C8) zx_tape::go_berserk();
bool fastload=false;
if (zx_tape::getplaying() && zx_tape::getOption(ZXTAPE_OPTION_FAST_LOAD)) { fastload=true; time = SDL_GetTicks(); }
while (zx_tape::getplaying() && zx_tape::getOption(ZXTAPE_OPTION_FAST_LOAD))
{
// zx_tape::update(z80::step());
uint8_t dt = z80::step();
t_states += dt;
zx_tape::update(dt);
if (SDL_GetTicks()-time>=1000) {
time = SDL_GetTicks();
zx_tape::report();
}
//zx_ula::sound_update(dt);
//zxscreen::refresh(dt);
}
if (fastload) { printf("%i\n", SDL_GetTicks()-time); t_states=0; }
// En cada bucle fem 10 pasos de la CPU, sino s'ofega
for (int i=0;i<5;++i) {
if (z80debug::isbreak(z80::getPC(), 9)) {
z80debug::stop();
zxscreen::redraw();
break;
} else {
uint8_t dt = z80::step();
t_states += dt;
zx_tape::update(dt);
zx_ula::sound_update(dt);
zxscreen::refresh(dt);
if (z80debug::debugging()) break;
}
}
if (t_states>=update_freq)
{
//if (SDL_GetTicks()>=time+1000)
//printf("%i\n", SDL_GetTicks()-(time+1000));
//else
// printf("%i\n", SDL_GetTicks()-(time+1000));
//t_states = 0;
//printf("%i: %i\n", SDL_GetTicks()-(time+1000), t_states);
while (SDL_GetTicks()<time+100) {}
t_states -= update_freq;
time = SDL_GetTicks();
z80analyze::refresh();
z80viewer::refreshAll();
}
z80analyze::refresh(true);
} else if (!z80debug::debugging() && z80debug::paused()) {
zxscreen::redraw(false);
ui::menu::show();
zxscreen::present();
}
ui::setClicked(false);
}
return 0;
}